[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bug#103980: g++-3.0: copies constructors



>  SFS fails to compile with gcc 3.0, and it was tracked down to this 
> example code that reproduces the error:

This is not a bug in the compiler, but in your code.

>   a << 10;

Here, a temporary of class aiosout must be initialized with an
expression of type aios. This is defined in section 8.5.3,
[dcl.init.ref]/3, where "cv1 T1" is "const aiosout" and "cv2 T2" is
"aios". Therefore, the two types are not reference compatible, and the
last section applies:

# Otherwise, a temporary of type cv1 T1 is created and initialized
# from the initializer expression using the rules for a non-reference
# copy initialization (8.5). The reference is then bound to the
# temporary.

So the temporary is initialized using copy initialization; the code is
rewritten as if

const aiousout tmp = a;
tmp << 10;

Since aiousout is a class type, this involves calling a copy
constructor, as if

const aiousout tmp(aiousout(a));

Now, 12.8, [class.copy]/15, allows to elide the copy constructor,
which g++ always did. 12.2, [class.temporary]/1, requires to still
perform access checks for the copy constructor, even if it is not
called:

# Even when the creation of the temporary object is avoided (12.8),
# all the semantic restrictions must be respected as if the temporary
# object was created.

Earlier g++ versions did not properly perform the access check for the
copy constructor that was not called; this bug has been corrected in
gcc 3.

To make your code work, I recommend to rewrite it as

aiosout tmp(a);
tmp << 10;

i.e. using direct initialization explicitly for a local variable.

Regards,
Martin



Reply to: