为什么 std::move 适用于常量对象
why std::move works with constant object
为什么下面的代码是合法的。根据我的理解,测试的构造函数参数 s1 指的是常量对象,在调用 std::move(( 之后,对象的状态应该改变,所以它应该给出错误但它的工作。
#include <iostream>
#include<memory>
#include <string>
using namespace std;
class test
{
string s;
public:
test( const string& s1) : s(std::move(s1))
{
}
};
int main()
{
test t1("data");
}
因为std::move
使移动对象成为可能。它实际上不会移动任何东西。那将是s
的移动构造函数。
但是由于此处移动的结果仍然是const
因此它不会调用移动构造函数。它将调用复制构造函数。
试试吧。你会知道的。
相关文章:
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- C++,如何使用常量对象和非常量对象进行比较?
- 通过从构造函数中的'this'复制的指针改变常量对象
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- C++ 对非常量对象的常量引用和对非常量对象的非常量引用之间的区别
- 算法不适用于非常量对象的const_iterator
- 为什么MSVC14允许声明指向动态未初始化常量对象的指针
- C++中的常量对象或私有/常量数据成员(变量)?
- 将值从指针复制到常量对象参数
- 指向静态常量对象的共享指针?
- 非常量对象的向量似乎在基于范围的 for 循环中被视为常量
- 将unique_ptr作为<Object>unique_ptr<常量对象返回>
- 对非常量对象的常量引用
- 为什么 std::move 适用于常量对象
- 如何最好地初始化和存储常量对象
- 构造常量对象与引用常量对象
- 指向从未变为常量的常量对象的C++指针
- 具有常量对象的代码大小
- 如何使构造函数接受一个非常量对象
- 我真的需要为常量对象实现用户提供的构造函数吗?