I noticed that aggregate list initalization of std::vector performs copy initialization when move is more applicable. At the same time, multiple emplace_backs do what I want.
I could only come up with this imperfect solution of writing a template function init_emplace_vector
. It's only optimal for non-explicit single-value constructors, though.
template <typename T, typename... Args>
std::vector<T> init_emplace_vector(Args&&... args)
{
std::vector<T> vec;
vec.reserve(sizeof...(Args)); // by suggestion from user: eerorika
(vec.emplace_back(std::forward<Args>(args)), ...); // C++17
return vec;
}
Question
Do I really need to use emplace_back in order to initialize std::vector as efficiently as possible?
// an integer passed to large is actually the size of the resource
std::vector<large> v_init {
1000, // instance of class "large" is copied
1001, // copied
1002, // copied
};
std::vector<large> v_emplaced;
v_emplaced.emplace_back(1000); // moved
v_emplaced.emplace_back(1001); // moved
v_emplaced.emplace_back(1002); // moved
std::vector<large> v_init_emplace = init_emplace_vector<large>(
1000, // moved
1001, // moved
1002 // moved
);
Output
Class large
produces information about copies/moves (implementation below) and so the output of my program is:
- initializer
large copy
large copy
large copy
- emplace_back
large move
large move
large move
- init_emplace_vector
large move
large move
large move
Large class implementation
My implementation of large
is simply a copyable/movable type holding a large resource that warns on copy/move.
struct large
{
large(std::size_t size) : size(size), data(new int[size]) {}
large(const large& rhs) : size(rhs.size), data(new int[rhs.size])
{
std::copy(rhs.data, rhs.data + rhs.size, data);
std::puts("large copy");
}
large(large&& rhs) noexcept : size(rhs.size), data(rhs.data)
{
rhs.size = 0;
rhs.data = nullptr;
std::puts("large move");
}
large& operator=(large rhs) noexcept
{
std::swap(*this, rhs);
return *this;
}
~large() { delete[] data; }
int* data;
std::size_t size;
};
Edit
By using reserve, there is no copy or move. Only large::large(std::size_t)
constructor is invoked. True emplace.
std::initializer_list
. – Jerseystd::array
. – Perpetualoperator=
which destroys the source is quite unusual and might cause unexpected problems. – Servilityinit_emplace_vector
could be improved withvec.reserve(sizeof...(Args))
– Irfanoperator=
does not destroy the source. It's the copy-swap idiom. – Beanerylarge& operator=(large& rhs)
here, however now it's not in the edit history. Yes,large& operator=(large rhs)
is fine, of course. – Servility