8

I suppose it's a g++ bug but I'm asking for confirmation.

The following code:

#include <complex>
#include <iostream>

int main()
{
    std::complex<double>  ac[10] = 23.5;

    for (int i = 0; i < 10; ++i)
        std::cout << ac[i] << ", ";

    std::cout << std::endl;

    return 0;
}

fails to compile on clang++ 3.5 with

"error: array initializer must be an initializer list")

but compiles, without errors or warnings, with g++ 4.9.2.

The output of the g++ compiled executable is

(23.5,0), (23.5,0), (23.5,0), (23.5,0), (23.5,0), (23.5,0), (23.5,0), (23.5,0), (23.5,0), (23.5,0),

so this array initialization

std::complex<double>  ac[10] = 23.5;

work as initialization of every element of the array with the value on the right of the equal sign (and, I suppose, could be useful, if it would a standard accepted feature)

If I substitute the std::complex<double> array with a double array

double  ac[10] = 23.5;

g++ gives an error similar to the clang++ one:

"error: array must be initialized with a brace-enclosed initializer".

Another case: an array of vectors

std::vector<double>  ac[10] = std::vector<double>(3, 0.5) ;

g++ compiles it (and initialize every vector in the array) and clang++ give the usual error.

So I suppose it's a g++ bug occurring only with initialization of array of class (struct?) types. I'm asking for confirmation.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
max66
  • 65,235
  • 10
  • 71
  • 111

0 Answers0