0xbadf00d
0xbadf00d

Reputation: 18178

Initialize a private aggregate from a braced-init-list

If I try to compile

class triangle
{
private:
    std::array<std::array<double, 2>, 3> m_vertices;
};

triangle const unit_triangle{ { { { 0, 0 }, { 1, 0 }, { 0, 1 } } } };

I get the error message

cannot convert from 'initializer list' to 'triangle'.

If I replace private by public, the code compiles successfully. Since I don't want to do that: What do I need to do, if I want to initialize unit_triangle from a braced-init-list?

Upvotes: 1

Views: 92

Answers (1)

Barry
Barry

Reputation: 303057

Having a private member prevents triangle from being an aggregate. It's being an aggregate that allows you to list-initialize a triangle without triangle having a constructor. That's why it works when you replace private with public.

To support the syntax you want, you simply need to add the appropriate constructor. In this case:

class triangle
{
public:
    triangle(std::array<std::array<double, 2>, 3> const& v)
        : m_vertices(v)
    { }

private:
    std::array<std::array<double, 2>, 3> m_vertices;
};

Upvotes: 3

Related Questions