为什么 std::move 当类型已经是 T&& 类型时
Why std::move when type is already of the type T&&
我今天在一些地方看到人们的回答,说我有Foo的构造函数。
Foo( std::vector<SomeType>&& data) noexcept : data_(std::move(data)) {};
为什么std::move(data)是必需的,因为std::vector<类型的数据不是;SomeType>,,
?std::move在我看来,它只是将数据强制转换为std::vector
http://en.cppreference.com/w/cpp/utility/move定义std::move为,
static_cast<typename std::remove_reference<T>::type&&>(t)
所以看起来std::move在这种情况下是无关的,但我可能遗漏了一些东西。
在这两种情况下,
void foo(const T& bar); // 1
void foo(T&& bar); // 2
bar
实际上是一个lvalue
。&&
的意思是重载2
将绑定到右值,例如在以下情况下:
foo(Bar());
而重载1
会绑定在这里:
T bar;
foo(bar);
在2
中,您仍然需要使用std::move
来使bar
看起来和行为像函数内部的rvalue
。
相关文章:
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 错误:无法传递非平凡类型"std::string"的对象和更多错误
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- std::函数指针错误:无法将 &A::a 转换为类型 std::function<>&&
- 包括<字符串>,使用不完整的类型"std::string"
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 错误:类型"std::__1::basic_string<char>"不提供呼叫运算符
- 错误:请求从"Mstream"转换为非标量类型"std::string {aka std::basic_string<char>}"
- 错误:移动文件时'QString'为非标量类型"std::__cxx11::字符串"
- CLion 无法解析类型 std::unordered_map,即使它提示我包含标头并且编译工作
- 字符串C :libc abi.dylib:终止未被发现的类型std :: out_of_range:basic
- 返回类型 std::optional<std::variant<...>>
- 变量具有不完整的类型std :: String;string.h有很多错误
- 对类型 'A *' 的非常量左值引用不能绑定到不相关的类型 'std::shared_ptr<A>' 的值
- 来自类型为std::basic_string::const_iterator的非常量引用的无效初始化,并且<char>来自类型std::basic_string<char>::
- 类型"std::array<char,6>"和"char"不兼容
- 此错误的含义是什么:从 'char* [40] 转换为非标量类型 'std::string
- 使用 TypedEq() 匹配类型 std::vector<std::vector>
- 如何修复错误:请求从"void"转换为非标量类型"std::vector<int>"