在这种情况下
struct Foo {};
Foo meh() {
return std::move(Foo());
}
Run Code Online (Sandbox Code Playgroud)
我很确定移动是不必要的,因为新创建的Foo将是一个xvalue.
但在这种情况下呢?
struct Foo {};
Foo meh() {
Foo foo;
//do something, but knowing that foo can safely be disposed of
//but does the compiler necessarily know it?
//we may have references/pointers to foo. how could the compiler know?
return std::move(foo); //so here the move is needed, right?
}
Run Code Online (Sandbox Code Playgroud)
我认为需要采取行动吗?
正如所料,以下代码无法编译.
#include <type_traits>
#include <utility>
int main()
{
using T = std::pair<const int, int>;
const auto ok = std::is_assignable<T, T>::value; // true
T x;
T y;
x = y; // compiler error
}
Run Code Online (Sandbox Code Playgroud)
但是以下三个编译器的值ok 是正确的.
为什么是这样?