[Bug c++/65815] std::array initialization with initializer list: a = {x,y,z} incorrectly flagged as syntax error

andras.aszodi at csf dot ac.at gcc-bugzilla@gcc.gnu.org
Tue Apr 21 10:48:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65815

--- Comment #5 from andras.aszodi at csf dot ac.at ---
If the array is a class member and it is initialized in-class then the
"single-brace" syntax gets flagged. Please try the following example:

// ----- file clarrinit.cc --
#include <array>
#include <iostream>

class Fred {
  public:
  const std::array<double, 2>& arr() const { return _a; }

  private:
  std::array<double, 2> _a = {1.0, 2.0}, // error
    _b{3.0, 4.0}, // error
    _c{{5.0, 6.0}}; // OK
};

int main(int argc, char *argv[]) {
  Fred f;
  std::cout << f.arr()[0] << std::endl;
}
// ----

Platform: Raspberry Pi 2, OS is Raspbian (not that it should matter IMO)
Command: `g++-4.9 -g -std=c++11 clarrinit.cc -o clarrinit`
Output: 
error: array must be initialized with a brace-enclosed initializer
   std::array<double, 2> _a = {1.0, 2.0},
                                       ^
clarrinit.cc:9:39: error: too many initializers for ‘std::array<double, 2u>’
clarrinit.cc:10:16: error: array must be initialized with a brace-enclosed
initializer
     _b{3.0, 4.0},
                ^
clarrinit.cc:10:16: error: too many initializers for ‘std::array<double, 2u>’


More information about the Gcc-bugs mailing list