C++备忘录062:Corner Cases for std::move/std::forward gist of "Effective Modern C++"

本文介绍了C++中std::move和std::forward的使用场景和注意事项,包括何时在返回值中使用它们,以及标准对于RVO的规定。强调了在可能需要移动的对象上避免声明const对象的重要性,以及不同情况下std::forward的行为。
摘要由CSDN通过智能技术生成
A sample implementation of std::move, which better be memorized as rvalue_cast
template <typename T>
decltype(auto) move(T&& param) {
   
    return static_cast<std::remove_reference_t<T>&&>(param);
}
Don’t declare objects const if you want to be able to move from them
#include <string>

struct S {
   
    S(const std::string text)
    : value(std::move(text)) {
   }

    std::string value;
}

std::string was copied, not moved. The move constructor takes an rvalue reference to a non-const std::string, and std::move(text) is a const std::string, it can be passed to the copy constructor, because an lvalue-reference-to-const is permitt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值