gpt4 book ai didi

c++ - 删除的构造函数 - MSVC 报告错误,Clang 没有

转载 作者:可可西里 更新时间:2023-11-01 16:36:09 27 4
gpt4 key购买 nike

考虑以下代码:

class SILPassPipelinePlan final {
public:
SILPassPipelinePlan() = default;
~SILPassPipelinePlan() = default;
SILPassPipelinePlan(const SILPassPipelinePlan &) = default;
SILPassPipelinePlan(SILPassPipelinePlan &&) = delete;

SILPassPipelinePlan x() {
SILPassPipelinePlan P;

return P;
}
};

int main() {
return 0;
}

MSVC 报如下错误:

1>consoleapplication2.cpp(13): error C2280: 'SILPassPipelinePlan::SILPassPipelinePlan(SILPassPipelinePlan &&)': attempting to reference a deleted function

1>consoleapplication2.cpp(8): note: see declaration of 'SILPassPipelinePlan::SILPassPipelinePlan'

Clang 和 GCC 没有。

从规范的角度来看,哪个编译器是正确的?这是 MSVC 错误还是 Clang 错误?

MSVC 来自最新的 Visual Studio 2015 Update 3,Clang 是 3.9.0 版本。

最佳答案

C++11 在某些场景中引入了隐式移动— yours included :

In the following copy-initialization contexts, a move operation might be used instead of a copy operation:

  • If the expression in a return statement ([stmt.return]) is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression, or

  • […]

overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If the first overload resolution fails, […]

Clang(唯一接受的实现,顺便说一句。)要么误解“失败”以包含已删除函数的选择,要么应用 [over.match.funcs]/8太松懈了。查看错误 31025 .

关于c++ - 删除的构造函数 - MSVC 报告错误,Clang 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283712/

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