删除构造 - MSVC报告错误,锵不

考虑下面的代码:删除构造 - MSVC报告错误,锵不

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'

锵和GCC没有。

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

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, […]

锵(唯一接受执行,顺便说一句)要么曲解“失败”,以包括删除功能的选择,或适用[over.match.funcs]/8太松懈了。查看错误31025。

回答:

Wandbox上的所有GCC版本均拒绝此代码。你是否有机会在Mac上测试它,并使用它的Clang-masquerading-as-GCC?

这与P0135无关。锵是简单地把一个过度自由的阅读“失败”在什么是目前[class.copy.elision]/3,它说,在这种情况下

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 or was not performed, [...], overload resolution is performed again, considering the object as an lvalue.

这重载不会失败;它成功并选择了正在被删除的移动构造函数。这应该是事情的结局。

这已报告为bug 31025。

以上是 删除构造 - MSVC报告错误,锵不 的全部内容, 来源链接: utcz.com/qa/266476.html

回到顶部