Imagine I want to construct a fixed-size std::vector
of objects without move or copy constructors, such as std::atomic<int>
. In this case the underlying std::atomic
class has a 1-arg constructor which takes an int
, as well as a default constructor (which initializes the value to 0).
Using the initializer_list
syntax like std::vector<std::atomic<int>> v{1,2,3}
doesn't work, because the arguments are first converted to the element type T
of the vector as part of the creation of the initializer_list
and so the copy or move constructor will be invoked.
In the particular case of std::atomic<int>
I can default-construct the vector and then mutate the elements after:
std::vector<std::atomic<int>> v(3);
v[0] = 1;
v[1] = 2;
v[2] = 3;
However, in addition to being ugly and inefficient, it isn't a general solution since many objects may not offer post-construction mutation equivalent to what you could get by calling the appropriate constructor.
Is there any way to get the "emplace-like" behavior that I want at vector construction?