Given the following C ++ 14 code:
struct A { };
A f(int);
A g(int);
A h(int);
const std::vector<A> v = { f(1), g(2), h(3) };
I know that A
in the initializer_list file is copied to the vector, instead of being moved (there are a lot of questions about this in stackoverflow).
My question is: how can I move them to a vector?
I was only able to make ugly IIFE (which supports v
const) and just avoids initializer_list:
const std::vector<A> v = []()
{
std::vector<A> tmp;
tmp.reserve(3);
tmp.push_back( f(1) );
tmp.push_back( g(2) );
tmp.push_back( h(3) );
return tmp;
}();
Can this be made elegant and efficient?
PD: v
should be std::vector<A>
for later use
source
share