jliv902
jliv902

Reputation: 1658

Move a vector without C++11

Let's say I have something like this:

struct Foo
{
    void Bar (std::vector <int> &vec) ;
    std::vector <int> m_vec ;
};

void Foo::Bar (std::vector <int> &vec)
{
    // Do stuff...

    m_vec = std::move (vec) ;
}

Is there any way, possibly with pointer trickery or std::swap that I would be able to simulate the m_vec = std::move (vec) ; line? Let's assume the vec being passed in was allocated on the stack.

Upvotes: 1

Views: 83

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477494

Maybe this:

void Bar(std::vector<int> & vec)
{
    m_vec.swap(vec);
    std::vector<int>().swap(vec);  // clear "vec"
}

Upvotes: 4

Related Questions