I want to make a mathematical vector class whose main data is an std::array. I also want to write the constructor to take in an std::array and initialize it to the data attribute. In my code I will template the type of elements and size of vector, but for now I will write code for a vector of four integers:
class MyVector {
private:
std::array<int,4> data;
public:
MyVector(std::array<int,4> input)
:data{ input } {}
}
And using implicit constructors I can define a MyVector object like:
MyVector v({0, 1, 2, 3});
My question is: is this initialization redundant? Am I creating an extra std::array only to through it away?
Does the compiler first create input from the list {0, 1, 2, 3} and then using the initializer list to copy that to the data attribute in the object? Or does the compiler just create the std::array in the memory address of the data attribute for the object. If I am creating an extra std::array, is there a more efficient way to input ints using the {} notation without creating an extra step of copying arrays? Remember, I want to accomplish this because I want the MyVector class to be templated, so the length of the {} is variable.
EDIT:
I have seen mentions of std::initializer_list. Is this essentially the same implantation of what I am doing or are there some compiler shortcuts? Can I use std::initializer_list for std::arrays? I don't want to use std::list because I want MyVector class to be instantiated on automatic memory and not free memory.