C++11中std::move的使用

 

右值引用

分清楚什么是左值和右值,一个比较简易的方法就是能否取地址,能取地址的则为左值

为什么引入右值机制?主要是为了效率。

右值一般为临时变量,字面常量也属于右值,因为临时变量往往在后面不可使用或者说不再使用,因此,我们可以steal窃取他们的资源。

 

移动构造

在C++11中,标准库在<utility>中提供了一个有用的函数std::move,std::move并不能移动任何东西,它唯一的功能是将一个左值强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义。从实现上讲,std::move基本等同于一个类型转换:static_cast<T&&>(lvalue);

std::move函数可以以非常简单的方式将左值引用转换为右值引用。

通过std::move,可以避免不必要的拷贝操作。

std::move是为性能而生

std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
 

场景:

  1. C++ 标准库使用比如vector::push_back 等这类函数时,会对参数的对象进行复制,连数据也会复制.这就会造成对象内存的额外创建, 本来原意是想把参数push_back进去就行了.
  2. C++11 提供了std::move 函数来把左值转换为xrvalue, 而且新版的push_back也支持&&参数的重载版本,这时候就可以高效率的使用内存了.
  3. 对指针类型的标准库对象并不需要这么做.

 

说明:

  1. value = std::move(t) 用来表明对象t 是可以moved from的,  它允许高效的从t资源转换到value上.
  2. 注意,标准库对象支持moved from的左值在moved 之后它的对象原值是有效的(可以正常析构),但是是unspecified的,可以理解为空数据,但是这个对象的其他方法返回值不一定是0,比如size().所以,moved from 之后的对象最好还是不要使用吧?(如有不正确理解,请告知)
  3. 对本身进行move,并赋值给本身是undefined的行为.
std::vector<int> v = {2, 3, 3};
v = std::move(v); // undefined behavior


std::move 的函数原型.

/**
 *  @brief  Convert a value to an rvalue.
 *  @param  __t  A thing of arbitrary type.
 *  @return The parameter cast to an rvalue-reference to allow moving it.
*/
template<typename _Tp>
  constexpr typename std::remove_reference<_Tp>::type&&
  move(_Tp&& __t) noexcept
  { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }



结构体 remove_reference 的原型,就是重载了多个结构体模板来获取原类型 type.

/// remove_reference
template<typename _Tp>
  struct remove_reference
  { typedef _Tp   type; };

template<typename _Tp>
  struct remove_reference<_Tp&>
  { typedef _Tp   type; };

template<typename _Tp>
  struct remove_reference<_Tp&&>
  { typedef _Tp   type; };

 

例子1
– 原lvalue值被moved from之后值被转移,所以为空字符串. 
– 摘录自cppreference

void TestSTLObject()
{
    std::string str = "Hello";
    std::vector<std::string> v;

    // uses the push_back(const T&) overload, which means
    // we'll incur the cost of copying str
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";

    // uses the rvalue reference push_back(T&&) overload,
    // which means no strings will be copied; instead, the contents
    // of str will be moved into the vector.  This is less
    // expensive, but also means str might now be empty.
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";

    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";

}

输出:

After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"
 

int test_move()
{
	std::string str1{ "abc" }, str2;
	fprintf(stdout, "str1: %s\n", str1.c_str()); // abc
	std::move(str1);
	fprintf(stdout, "str1: %s\n", str1.c_str()); // abc
	str2 = std::move(str1);
	fprintf(stdout, "str1: %s\n", str1.c_str()); // 
	fprintf(stdout, "str2: %s\n", str2.c_str()); // abc
 
	return 0;
}

参考:

https://blog.csdn.net/swartz_lubel/article/details/59620868

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值