右值引用
分清楚什么是左值和右值,一个比较简易的方法就是能否取地址,能取地址的则为左值。
为什么引入右值机制?主要是为了效率。
右值一般为临时变量,字面常量也属于右值,因为临时变量往往在后面不可使用或者说不再使用,因此,我们可以steal窃取他们的资源。
移动构造
在C++11中,标准库在<utility>中提供了一个有用的函数std::move,std::move并不能移动任何东西,它唯一的功能是将一个左值强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义。从实现上讲,std::move基本等同于一个类型转换:static_cast<T&&>(lvalue);
std::move函数可以以非常简单的方式将左值引用转换为右值引用。
通过std::move,可以避免不必要的拷贝操作。
std::move是为性能而生。
std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝。
下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
场景:
- C++ 标准库使用比如vector::push_back 等这类函数时,会对参数的对象进行复制,连数据也会复制.这就会造成对象内存的额外创建, 本来原意是想把参数push_back进去就行了.
- C++11 提供了std::move 函数来把左值转换为xrvalue, 而且新版的push_back也支持&&参数的重载版本,这时候就可以高效率的使用内存了.
- 对指针类型的标准库对象并不需要这么做.
说明:
- value = std::move(t) 用来表明对象t 是可以moved from的, 它允许高效的从t资源转换到value上.
- 注意,标准库对象支持moved from的左值在moved 之后它的对象原值是有效的(可以正常析构),但是是unspecified的,可以理解为空数据,但是这个对象的其他方法返回值不一定是0,比如size().所以,moved from 之后的对象最好还是不要使用吧?(如有不正确理解,请告知)
- 对本身进行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;
}
参考: