c++ - 我可以用什么代替 std::move()?

标签 c++ c++11 std rvalue-reference move-semantics

我正在使用符合 C++0x 规范的 C++ 编译器,并希望为环绕 std::wstring 的 String 类创建 move 构造函数。

class String {
public:
    String(String&& str) : mData(std::move(str.mData)) {
    }

private:
    std::wstring mData;
};

在 Visual Studio 中,这可以完美地工作。在 Xcode 中,std::move() 不可用。

最佳答案

std::move 只是将其参数转换为右值引用。您可以编写自己的版本:

template<class T>
typename std::remove_reference<T>::type&&
move( T&& arg ) noexcept
{
  return static_cast<typename std::remove_reference<T>::type&&>( arg );
}

关于c++ - 我可以用什么代替 std::move()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553841/

相关文章:

c++ - 一种用于 POD 的模板特化,一种用于类层次结构和其他情况下的错误?

c++ - 将一种类型的 constexpr n 维数组转换为不同类型的 constexpr n 维数组

c++ - std::map 索引运算符与插入方法的性能

c++ - 为什么 C++ 迭代器需要返回引用?

c++ - 我可以在 Visual C++ 2010 中导入 QT .pro 文件吗

c++ - undefined symbol -在centos 7中使用log4cxx

c++ - 回调函数 : Incompatible argument

c++ - 使用数学将 c++ 字符串加倍

c++ - 从 std::function 创建一个 boost::python::object

c++ - 将 csv 文件的一行拆分为 std::vector?