lundi 1 octobre 2018

Mixed list initialization in C++11

With reference to c++11 list initialization, may I initialize a list with an element and another list?

Let's say I have the following code:

#include <vector>

class Foo
{
    public:
        Foo(int value){m_v=value;}
    private:
        int m_v = 0;
};

int main()
{
   std::vector<Foo> v1, v2, v3;
   v1 = {Foo(1)}; //ok
   v2 = {Foo(2), Foo(3)}; //ok
   v3 = {Foo(3), v2}; //error: no match for ‘operator=’ (operand types are ‘std::vector’ and ‘’)
}

Is there a way to create in one line of code, using list initialization, a vector made of the element of another vector plus a new element (a prepend, in the example above).

Aucun commentaire:

Enregistrer un commentaire