我很好奇为什么我不能编译以下代码.
这是无意义的代码(我知道),但我最初在使用具有完美转发等模板的其他代码中遇到了问题.
我设法将问题缩小到std::move/ std::forward/ std::remove_reference,我很好奇为什么它首先需要一个临时的...
#include <utility>
#include <stdio.h>
struct Foo {
Foo(Foo&& other) {
printf("Foo::(Foo&&) %p\n", this);
}
Foo() {
printf("Foo::() %p\n", this);
}
~ Foo() {
printf("Foo::~Foo() %p\n", this);
}
};
void test(Foo&& t)
{
// OK: Works fine and "f1" is is constructed with Foo::Foo(Foo&& other)
// Foo f1 = std::move(t);
// ERROR: Here it is trying to bind a temporary Foo to a non-const lvalue
// I can't figure out why it …Run Code Online (Sandbox Code Playgroud)