Given the following C++14 code:
struct A { /* heavy class, copyable and movable */ }; // complex code to create an A A f(int); A g(int); A h(int); const std::vector<A> v = { f(1), g(2), h(3) }; I know the A's in the initializer_list are copied into the vector, instead of being moved (there are a lot of questions in stackoverflow about this).
My question is: how can I move them into the vector?
I've only been able to do the ugly IIFE (that keeps 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; }(); Is it possible to make this elegant and efficient?
PD: v has to be a std::vector<A> for later use
{ f(1), g(2), h(3) }will never be used again, so its items should be moveable-from. It's frustrating to be repeatedly told that's impossible because of the reusability ofstd::initializer_listor w/e, when actually we should not have to care about that.initializer_listis awful