c++/3615: unneccessary temporary instance created during function call

niemayer@isg.de niemayer@isg.de
Mon Jul 9 03:56:00 GMT 2001

>Number:         3615
>Category:       c++
>Synopsis:       unneccessary temporary instance created during function call
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jul 09 03:56:00 PDT 2001
>Originator:     Peter Niemayer
>Release:        gcc-3.0
When calling a function that takes a reference to a base
class "base" with an argument that is an instance of a
class that can be casted to a "class foo &" (where foo
is derived from "base"), gcc3 tries to create a temporary
"foo" instance without any reason and failing if copying
of "foo" isn't allowed.
compiling the following small code example with gcc2 works
fine, with gcc-3.0 it results in the error:

bug.cxx: In function `void bug()':
bug.cxx:26: `foo::foo(const foo&)' is private
bug.cxx:47: within this context
bug.cxx:47:   initializing temporary from result of `Ptr<T>::operator T&() 
   [with T = foo]'
bug.cxx:38: in passing argument 1 of `void func2(base&)'
---------- code sample -----------

template <class T>
class Ptr {
	T * ptr;
	Ptr(void) : ptr(0) { };
	Ptr(T * p) : ptr(p) { };

	~Ptr(void)	{ delete ptr; }

	operator T & () { return *ptr; }

class base {
	base(void) { }
	~base(void) { }

class foo : public base {
	foo(const foo & rv); // does not exist - no copy allowed!
	foo(void) { }
	~foo(void) { }

void func(foo & f) {
	// ...

void func2(base & b) {
	// ...

void bug(void) {
	Ptr<foo> f = new foo;
	func(f); // works as expected with gcc2 and gcc3
	func2(f); // works with gcc2, creates an unneccessary
	          // temporary instance with gcc-3.0

---------- code sample end ------------


More information about the Gcc-bugs mailing list