从类移动向量(与 2013 编译器相比)
moving vector from class (vs 2013 compiler)
我想从类中移动向量
class Data
{
public:
std::vector<int> && getValues() {return std::move(values);}
private:
std::vector<int> values;
};
我使用 vs2013 编译器,据我所知,它不支持 ref 限定符。如何移动安全?
Data d;
std::vector<int> v1;
std::vector<int> v2;
...
v1=d.getValues(); //i want copy
v2=std::move(d.getValues()); // i want move
只需通过正常引用返回:
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
相关文章:
- 从类移动向量(与 2013 编译器相比)
- Visual Studio 2013中可能的C/C 编译器错误
- 如果使用 Visual C++ 编译器 2013 年 11 月 CTP,是否还需要使用它编译所有依赖项
- Visual Studio 2013的定制编译器
- 即使在 Visual C++ 编译器中,在模板中传递 "const char" 参数也有些麻烦 2013 年 11 月 CTP
- 在使用 Visual Studio 2013 64 位编译器编译的 CMake 项目中使用 ttmath
- 什么是 ?和 @ 符号演示此编译器错误?- Visual Studio 2013 Compiler.
- 如何防止输出屏幕在 Visual Studio 2013 C++编译器中消失
- static_assert(std::is_abstract)在Visual Studio 2013中导致编译器错误
- 错误 C2601:'main':本地函数定义非法 - MS VS 2013 编译器
- Visual Studio 2013中的编译器错误C2758
- 开始使用 clang 3.6.0 和 Visual Studio 2013;如何解决"unknown argument: -ftemplate-depth"编译器错误?
- 如何使用Visual Studio 2012和“英特尔编译器XE 2013”构建可在Windows XP中运行的可执行文
- boost::fusion::调用Visual Studio 2013的编译器错误
- Visual Studio在转换为2013的项目上使用较旧的2012编译器
- Visual Studio 2013有符号/无符号整数数学行为不同时,求值表达式作为函数参数,编译器切换
- 带有模板返回类型的虚基函数:编译器在使用pointtype作为模板参数的派生类时失败(MSVC 2013)
- 使用 Visual Studio 2013 的 C 编译器进行 MATLAB
- Visual Studio 2013 for Targeting XP的Visual C++编译器标志
- 使用Visual c++编译器2013年11月CTP With Origin c++ 11库