This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Binding a temporary to a const reference



> It seems that g++ tries to call a class' copy constructor when binding
> to a const reference:

While I believe you are correct in saying that g++ is wrong in this case,
the protection rule is independent of whether or not the copy constructor
is actually called.

> ------
> class A {
>     A (A const&);
>     A operator = (A const&);
> 
>   public:
>     A() {}
>     template <typename T> operator T* () const { return 0; }
> };
> 
> A const& a = A();    // Error?

This doesn't seem to represent a use of the copy constructor, so if
I understand things correctly, g++ should accept it.  We create a
temporary with the default constructor, then assign a reference to
it; no implicit copy is ever made.

Note, though, that

A tmp = A();

is definitely an error, even though g++ can (and does, in other
circumstances) optimize away the copy constructor and build tmp
directly from A::A.  I'm guessing that the fact that this rule
must be applied in some circumstances where the copy constructor
is not actually called is the reason for this error.

> 
> This is what I get on mips-sgi-irix6.5 (and with egcs 1.1.2 on
> i686-pc-linux-gnu):
> 
> 
> $ g++ --version
> 2.95.2
> $ g++ -ansi -pedantic -W -Wall -c bugp.cc
> bugp.cc: In function `void
> __static_initialization_and_destruction_0(int, int)':
> bugp.cc:2: `A::A(const A &)' is private
> bugp.cc:10: within this context
> 
> 
> I don't see why a copy constructor would have to be accessible in this
> case, and I found nothing in the standard.
> 
> So is this a g++ bug?
> 
>     Brane
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]