Question

Is this a g++ bug?

#include <array>

struct inherit : std::array<int , 3>{
    using std::array<int , 3>::array;
};

std::array<int, 3> ok1 = {1,2,3};
inherit ok2;
inherit bad = {1,2,3};

Instantiating bad, I get error: could not convert ‘{1, 2, 3}’ from ‘<brace-enclosed initializer list>’ to ‘inherit’. It looks absolutely correct to me.

Was it helpful?

Solution

std::array is an aggregate. It has no user defined constructors.

From the C++ Standard

1 An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

class inherit is not an aggregate because it has a base class. So it may not use the brace initialization (except an empty initializer list). You should explicitly define constructors for class inherit if you want to initialize it with a non empty initializer list,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top