optimization bug? in initialization of class variable

Andrew J. Malton ajmalton@uwaterloo.ca
Fri Dec 14 08:09:00 GMT 2001


Is this a bug or a very wierd valid interpretation of the standard?

Let T be a nonunion class type.

In the case of a declaration
	T v = e;
if e is not suitable for copy initialization, then the declaration means
	T v (T(e));
where the T(e) denotes a temporary created with suitable conversion 
construction [8.5 14].  So this should invoke a conversion followed by a 
copy.   Both constructors have to be accessible [12.2 1].  Creation of 
the temporary T(e) may be `avoided' [12.2 2 example] but semantic 
restrictions (acessibility) must still be satisfied.

In the attached example, GCC (2.95, also 3.0) checks accessibility of 
the constructors (verify by uncommenting the "private:"), but avoids 
creating the temporary, even though its constructor has a side effect.  
The program outputs 0.  I am surprised.  Is the theory that 
accessibility is a "semantic restriction" but the side effect is merely 
"semantics"?  Or is this a bug plain and simple?

-- cut here --

#include <iostream>

class C
{
     int x;
public:
     C (const int n) : x(n) { }
//private:
     C (const C& c) : x(c.x) { ++ copied; }
public:
     static int copied;
};

int C::copied = 0;

int main ()
{
     C c = 5;
     std::cout << C::copied << std::endl;
}



--
Dr. Andrew Malton, Associate Professor
Department of Computer Science
University of Waterloo
Waterloo, Canada N2L 3G1

http://www.cs.uwaterloo.ca/~ajmalton
tel: +1 519 888 4567 x 5144
fax: +1 519 885 1208



More information about the Gcc mailing list