mercredi 30 mars 2016

C++11 making variadic constructor understand an initialization list of initialization lists

Let's say I have a class of Point and and an array of Points, where the number of points is given by a template parameter. How do I get initialization to work using braces? If I use:

class Point {
public:
    float x, y;
    Point(float x, float y) : x(x), y(y) {}
};

template <size_t N>
class Array {
private:
    std::array<Point, N> _points;

public:
    template <typename... Points>
    Array(const Points& ... points) : _points({ points... }) {
    }
};

Then this works:

Array<2> a{Point{1,1}, Point{2, 2}};

But If I don't provide explicit Point objects, I get an error in Xcode:

Array<2> c{{1,1}, {2, 2}};

The error is: "No matching Constructor for initialization of Array<2>". For the particular constructor it says "Candidate constructor not viable: requires 0 arguments, but 2 were provided".

How do I get this to work?

Aucun commentaire:

Enregistrer un commentaire