相关疑难解决方法(0)

为什么在const对象上调用std :: move会在传递给另一个对象时调用复制构造函数?

为什么在const对象上调用std :: move会在传递给另一个对象时调用复制构造函数?具体来说,代码

#include <iostream>

struct Foo {
    Foo() = default;
    Foo(Foo && x) { std::cout << "Move" << std::endl; }
    Foo(Foo const & x) = delete;
};

int main() {
    Foo const x; Foo y(std::move(x)); 
}
Run Code Online (Sandbox Code Playgroud)

无法使用消息进行编译:

g++ -std=c++14 test07.cpp -o test07
test07.cpp: In function 'int main()':
test07.cpp:10:36: error: use of deleted function 'Foo::Foo(const Foo&)'
     Foo const x; Foo y(std::move(x)); 
                                    ^
test07.cpp:6:5: note: declared here
     Foo(Foo const & x) = delete;
     ^
Makefile:2: recipe for target 'all' …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

23
推荐指数
2
解决办法
4928
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1