Search code examples
c++c++11move

How can I move a `std::vector` member variable to the caller of a method?


Please consider the following piece of code

class A
{
public:
    A(std::size_t d)
        : m_v(d)

    std::vector<double> operator()() {
        return m_v;
    }

private:
    std::vector<double> m_v;
};

I want to move m_v to the caller of operator() instead of copying it. What do I need to do? Simply write return std::move(m_v) and change the return type to std::vector<double>&&?


Solution

  • It is enough to write return std::move(m_v).