dimanche 29 novembre 2015

'initializing': cannot convert from 'Array

Consider the following code:

#include <cstddef> //for std::size_t

template<class T, std::size_t Size>
class Array
{
private:
    T _data[Size];
public:
    template<class... Args>
    Array(Args&&... vals)
        : _data{ vals... }
    {}
};

int main()
{
    Array<int, 3> a = { 1, 2, 3 };
    Array<int, 3> b = { 4, 5, 6 };

    Array<Array<int, 3>, 2> arr = { a, b };
}

Everything works as expected for the first two objects (a and b) of the class.

Then I declare an array of arrays (2D array if you wish). So when the constructor of Array<Array<int, 3>, 2> arr; is executed I believe the template parameter T will be equal to Array<int, 3>. That way we should be able to successfully give Args&&... other objects of the same type. Then the parameter pack will expand.

So Array<Array<int, 3>, 2> arr; should essentially have a private member:

Array<int, 3> _data[2];

Apparently not, since I get the error that is in the title.

Aucun commentaire:

Enregistrer un commentaire