gpt4 book ai didi

C++11 "Non-movable"类型

转载 作者:可可西里 更新时间:2023-11-01 15:14:55 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why do C++11-deleted functions participate in overload resolution?

我对以下 C++11 代码有两个问题:

#include <iostream>

using namespace std;

struct A {
A() { cout << "Default c-tor" << endl; }
A(const A&) { cout << "Copy c-tor" << endl; }
A(A&&) = delete;
};

A f()
{
A a;
return a;
}

int main()
{
A b = f();
return 0;
}

我用 gcc 和 clang 得到以下编译错误

gcc-4.7.2 (g++ --std=c++11 main.cpp):

main.cpp: In function ‘A f()’:
main.cpp:16:9: error: use of deleted function ‘A::A(A&&)’
main.cpp:8:2: error: declared here
main.cpp: In function ‘int main()’:
main.cpp:21:10: error: use of deleted function ‘A::A(A&&)’
main.cpp:8:2: error: declared here

clang-3.0 (clang++ --std=c++11 main.cpp):

main.cpp:19:4: error: call to deleted constructor of 'A'
A b = f();
^ ~~~
main.cpp:8:2: note: function has been explicitly marked deleted here
A(A&&) = delete;
^
1 error generated.
  • 如果显式删除了 move 构造函数,编译器是否应该使用复制构造函数?
  • 有谁知道“不可 move ”类型的用途吗?

提前致谢。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com