I tried to do the following :
template <typename T, int N>
struct Vector {
T v[N];
template<typename... Args> Vector(Args... args) : v { args... } {}
template<typename S> Vector(Vector<S, N> const & V) : v {V.v} {}
};
int main() {
Vector<float, 4> V (1.0f, 2.0f, 3.0f, 4.0f);
Vector<float, 4> V2 (V);
for (auto f : V2.v) { cout << f << ", "; } cout << endl;
return 0;
}
And it worked (printed "1, 2, 3, 4, "), so I did not suspect anything until I tried to "specialize it" with :
Vector(Vector const & V) : v {V.v} {}
or to use it with :
Vector<double, 4> V2 (V);
And the compiler said :
error: cannot convert 'const float*' to 'float' in initialization
or the same with 'double'.
After that I tried simple arrays, and it failed with the same error, yet with enough templating it works..
Can someone please explain to me what is going on here?
v1.v = v2.v;std::array. It is practically the same as using an array, without all of the pitfalls you're seeing now, as this example shows