c++ - std::move、std::forward、值类型和模板推导

标签 c++ templates c++11

假设我有这段代码

template <typename T> void Swap(T&& a, T&& b) {
    T tmp = std::move(a);
    a = std::move(b);
    b = std::move(tmp);
}

int main()
{
    int a = 2;
    int b = 3;
}

据我了解this talk , 当打电话时 Swap(a, b) ,编译器应该推断出 T&& 的事实应该是 T&并转换它。但在这种情况下,GCC 会给我以下错误:

error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'std::remove_reference<int&>::type {aka int}' 
T tmp = std::move(a);

我要么必须调用 Swap使用 Swap(std::forward<int>(a), std::forward<int>(b))Swap(std::move(a), std::move(b)) ,或替换 Swap Swap(T& a, T& b) 签名.

为什么会这样?这里的正确用法是什么?

最佳答案

你需要这个:

template <typename T> void Swap(T&& a, T&& b)
{
    using U = typename std::remove_reference<T>::type;

    U tmp = std::move(a);
    a = std::move(b);
    b = std::move(tmp);
}

正如您在问题中暗示的那样,在您的示例中,T 被推导为 int&,并且初始化 int& tmp = std::move(a ); 格式错误。

关于c++ - std::move、std::forward、值类型和模板推导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38379307/

相关文章:

c++ - 在无序映射中实现无序映射

java - Wicket:<body> 中模板化的 javascript?

c++ - 编译器会为 ref 参数实例化一个新的模板函数吗?

c++ - 无法使用带有 g++ 的 -std 标志进行编译

c++ - std::thread :如何在类主体中将线程声明为普通成员?

c++ - 如何在不使用 DirectX 或 OpenGL 的情况下渲染游戏?

c++ - C++ 桌面应用程序中的网页

c++ - 大量使用移动平台模板

c++ - 类成员函数参数列表是否可能依赖于模板参数?

c++ - 使用 VC141 将 high_resolution_clock::time_point 转换为 time_t