c++/4696: Casting operator creates bogus temporary

sparker@cs.utah.edu sparker@cs.utah.edu
Thu Oct 25 23:16:00 GMT 2001


>Number:         4696
>Category:       c++
>Synopsis:       Casting operator creates bogus temporary
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 25 23:16:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Steven Parker
>Release:        3.0.2
>Organization:
University of Utah
>Environment:
System: Linux ufo.cs.utah.edu 2.4.8-pre3-xfs #22 Thu Oct 18 14:10:22 MDT 2001 i686 unknown
Architecture: i686

	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3.0.2/configure --prefix=/usr --enable-languages=c++,f77
>Description:

gcc 3.0 and 3.0.2 attempt to produce an unnecessary copy when using
a casting operator.  gcc 2.95 did not have this problem, and other
C++ compilers accept this code.

Here is a simple example:

class A {
public:
  A() {}
private:
  A(const A&);
};

class B : public A {
public:
  B() {}
private:
  B(const B&);
};

class C {
  B b;
public:
  operator B&() {return b;}
};

void foo1(const B& b)
{
}

void foo2(const A& a)
{
}

main()
{
  A a;
  foo2(a); // Good
  B b;
  foo1(b); // Good
  foo2(b); // Good
  C c;
  foo1(c); // Good
  foo2(c); // Bad: gcc complains about private copy ctor in B
  foo2(static_cast<B&>(c)); // Workaround
};

The line marked Bad: complains on gcc 3.0.* that the copy
ctor for B is private, which is true.  However, the copy ctor
for B should never be needed - a temporary object is not needed
in this case.

This code compiles under gcc 2.9x, SGI MIPSpro 7.3.1.1m, and
Compaq C++ V6.3-008.

	
>How-To-Repeat:
   g++ -o t t.cc
>Fix:
        Workaround is an explicit cast (yuck)
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-prs mailing list