std::move 和 std::forward

关联点

都是执行转换(cast)的函数(函数模板),不产生任何可执行代码。且都可以把实参转换成右值。

std::move无条件将实参(const除外 )转换成右值引用,std::forward 条件返回右值引用

python 复制代码
_EXPORT_STD template <class _Ty>
_NODISCARD _MSVC_INTRINSIC constexpr remove_reference_t<_Ty>&& move(_Ty&& _Arg) noexcept {
    return static_cast<remove_reference_t<_Ty>&&>(_Arg);
}
python 复制代码
_EXPORT_STD template <class _Ty>
_NODISCARD _MSVC_INTRINSIC constexpr _Ty&& forward(remove_reference_t<_Ty>& _Arg) noexcept {
    return static_cast<_Ty&&>(_Arg);
}

_EXPORT_STD template <class _Ty>
_NODISCARD _MSVC_INTRINSIC constexpr _Ty&& forward(remove_reference_t<_Ty>&& _Arg) noexcept {
    static_assert(!is_lvalue_reference_v<_Ty>, "bad forward call");
    return static_cast<_Ty&&>(_Arg);
}
python 复制代码
_EXPORT_STD template <class _Ty>
struct remove_reference {
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty;
};

template <class _Ty>
struct remove_reference<_Ty&> {
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty&;
};

template <class _Ty>
struct remove_reference<_Ty&&> {
    using type                 = _Ty;
    using _Const_thru_ref_type = const _Ty&&;
};

_EXPORT_STD template <class _Ty>
using remove_reference_t = typename remove_reference<_Ty>::type;

先去引用,然后转换成右值

区别

1. 用途不同

  • std::move
    无条件将左值转换为右值引用,表示对象资源可被"移动"。用于触发移动构造函数或移动赋值运算符,避免深拷贝。
    示例:
cpp 复制代码
std::string s1 = "Hello";
std::string s2 = std::move(s1); // s1 的资源被移动到 s2,s1 不再有效
  • std::forward
    有条件地保持值类别(左值/右值),用于完美转发。通常与万能引用(T&&)配合,在泛型代码中保持参数的原始类型。
    示例:
cpp 复制代码
template<typename T>
void wrapper(T&& arg) {
    callee(std::forward<T>(arg)); // 保持 arg 的原始值类别
}

2. 转换条件

  • std::move
    无论输入是左值还是右值,始终返回右值引用。
cpp 复制代码
auto rval = std::move(lval); // 无条件转为右值
  • std::forward
    根据模板参数 T 的类型决定转换行为:
cpp 复制代码
forward<T>(arg); // 类型 T 决定结果
复制代码
- 若 `T` 是左值引用(如 `int&`),返回左值引用。  
- 若 `T` 是非引用或右值引用(如 `int` 或 `int&&`),返回右值引用。

3. 实现机制

  • std::move** 实现**
cpp 复制代码
template<typename T>
constexpr typename std::remove_reference<T>::type&& move(T&& t) noexcept {
    return static_cast<typename std::remove_reference<T>::type&&>(t);
}

直接通过 static_cast 将输入转换为右值引用。

  • std::forward** 实现**
cpp 复制代码
template<typename T>
constexpr T&& forward(typename std::remove_reference<T>::type& t) noexcept {
    return static_cast<T&&>(t);
}

根据 T 的类型推断结果,决定返回左值或右值引用。


4. 应用场景

  • 使用 std::move 的场景
    • 需要明确转移对象资源所有权时(如实现移动构造函数)。
    • 避免拷贝开销,例如将局部变量移动到容器中:
cpp 复制代码
std::vector<std::string> vec;
std::string s = "data";
vec.push_back(std::move(s)); // 移动而非拷贝
  • 使用 std::forward 的场景
    • 泛型函数模板中转发参数,保持其原始值类别:
cpp 复制代码
template<typename... Args>
void emplace(Args&&... args) {
    container.emplace_back(std::forward<Args>(args)...); // 完美转发参数包
}

总结

特性 std::move std::forward
目的 强制转为右值,触发移动语义 保持参数原始值类别,完美转发
转换条件 无条件 依赖模板参数 T
典型应用 移动构造函数、避免拷贝 泛型代码中的参数转发
参数类型 接受任意类型 通常与万能引用 T&& 配合使用

补充:

//拷贝构造

MyStruct(const Mystruct &)

{

}

//移动构造

Mystruct( Mystruct &&) // 无const

{

}

万能引用:

T&&

相关推荐
Dream it possible!1 天前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
爱和冰阔落1 天前
【C++继承下】继承与友元 / static 菱形继承与虚继承 组合的详解分析
c++·面试·腾讯云ai代码助手
草莓熊Lotso1 天前
《C++ Stack 与 Queue 完全使用指南:基础操作 + 经典场景 + 实战习题》
开发语言·c++·算法
敲上瘾1 天前
单序列和双序列问题——动态规划
c++·算法·动态规划
ajassi20001 天前
开源 C++ QT QML 开发(二十二)多媒体--ffmpeg编码和录像
c++·qt·开源
小糖学代码1 天前
Linux:11.线程概念与控制
linux·服务器·c语言·开发语言·c++
Larry_Yanan1 天前
QML学习笔记(四十)QML的ApplicationWindow和StackView
c++·笔记·qt·学习·ui
Kratzdisteln1 天前
【C语言】Dev-C++如何编译C语言程序?从安装到运行一步到位
c语言·c++
Dream it possible!1 天前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表
kyle~1 天前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++