Search code examples
c++c++14moveinitializer-list

Efficient and elegant way to initialize vector


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


Solution

  • Not sure if it counts as "elegant" but you could use an array (or std::array) which uses aggregate initialization that doesn't suffer from this problem and move_iterator to move the values into the vector.

    std::array<A, 3> init = { f(1), g(2), h(3) };
    
    std::vector<A> v{std::make_move_iterator(init.begin()), 
                     std::make_move_iterator(init.end())};
    

    Live demo.