user3144405
user3144405

Reputation:

Constructor is not inherited even using `using`

I'm wondering why the following code does not compile. This is only a showcase example, I'm not arguing about the inheritance of standard containers etc.

class A : public std::array<int,3>
{
public:
    using std::array<int,3>::array;

    // define other methods (no data members)
};

int main(int argc, char **argv) {        
    A a ({1,2,3});      
    return 0;
}

The compiler (g++) complains because it can only find the default constructor A(), eventhough I think I did what needed be to inherit the constructors from std::array.

Would someone please care to explain why it acts so and how I could cope with that problem without redefining and forwarding by hand the implicit constructor(s)?

Of course the code

using A = std::array<int,3>

int main(int argc, char **argv) {        
    A a ({1,2,3});      
    return 0;
}

compiles fine, but I need to add some home-made operators to fit my purposes.

Thanks for your help.

Upvotes: 0

Views: 73

Answers (2)

selalerer
selalerer

Reputation: 3924

The braced list you provided to the constructor is considered as one parameter. The compiler is looking for a constructor with one parameter of class A. This parameter must be able to accept a braced list of 3 integers for it to be used. Since there's no such constructor for class A the compiler issues an error.

Upvotes: 0

galop1n
galop1n

Reputation: 8824

std::array do not have constructors except the automatic ones to keep it an aggregate and having a base class is a rejection of the aggregate initialization rules as explain here : http://en.cppreference.com/w/cpp/language/aggregate_initialization

Upvotes: 1

Related Questions