Initialisation lists and -ansi -pedantic

Niall Smart njs3@doc.ic.ac.uk
Wed May 20 08:39:00 GMT 1998


Hi,

Consider the following C++ code fragment:

     1  #include <iostream.h>
     2  
     3  struct foo {
     4          double  d;
     5          char    c;
     6  };
     7  
     8  int main()
     9  {
    10          foo     x = { 1.34, 'p' };
    11          foo     y = { 3.22, 'q' };
    12          foo     f[2] = { x, y };
    13          
    14          cerr << f[0].d << " " << f[0].c << endl;
    15          cerr << f[1].d << " " << f[1].c << endl;
    16  }

Under egcs and gcc this code compiles and behaves as ``expected''
both with and without `-Wall -ansi -pedantic'.

My question is: is line 12 ANSI C++ compliant?  SUNWspro CC 4.2 does
not accept this line, complaining:

"foo.cc", line 12: Error: Cannot use foo to initialize double.
"foo.cc", line 12: Error: Cannot use foo to initialize char.

Placing braces around x and y has no effect. As expected it accepts:

     12         foo     f[2] = { { x.d, x.c} , { y.d, y.c } };

I can't find a discussion of this in the TC++PL 3Ed anywhere.

Niall




More information about the Gcc-bugs mailing list