从 class 移动矢量(对比 2013 编译器)

moving vector from class (vs 2013 compiler)

我想从 class

移动矢量
class Data
{
    public:
        std::vector<int> && getValues() {return std::move(values);}

    private:
        std::vector<int> values;
};

我使用 vs2013 编译器,据我所知,它不支持引用限定符。怎么搬家安全?

Data d;
std::vector<int> v1;
std::vector<int> v2;
...
v1=d.getValues(); //i want copy
v2=std::move(d.getValues());  // i want move

只是 return 按正常参考:

class Data
{
public:
    const std::vector<int>& getValues() const {return values;}
    std::vector<int>& getValues() {return values;}

    // And if you really want to move member, you may do
    std::vector<int> takeValues() {return std::move(values);}
private:
    std::vector<int> values;
};

那么你可以使用

Data d;
std::vector<int> v1;
std::vector<int> v2;
//...
v1 = d.getValues(); // copy
v2 = std::move(d.getValues()); // move
// Or alternatively:
v2 = d.takeValues(); // move