小编Rui*_*Fig的帖子

std :: move(T &&)和临时对象.临时来自哪里?

我很好奇为什么我不能编译以下代码.

这是无意义的代码(我知道),但我最初在使用具有完美转发等模板的其他代码中遇到了问题.

我设法将问题缩小到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)

c++ c++11

9
推荐指数
2
解决办法
698
查看次数

标签 统计

c++ ×1

c++11 ×1