在保证复制省略的情况下,为什么需要完全定义类? [英] With guaranteed copy elision, why does the class need to be fully defined?
问题描述
此帖子的后续操作.请考虑以下内容:
class C;
C foo();
那是一对有效的声明.仅声明一个函数时,无需完全定义C
.但是,如果我们要添加以下功能:
That is a pair of valid declarations. C
doesn't need to be fully defined when merely declaring a function. But if we were to add the following function:
class C;
C foo();
inline C bar() { return foo(); }
然后突然C
需要是一个完全定义的类型.但是在保证复制省略的情况下,不需要任何成员.没有复制甚至没有移动,该值在其他位置初始化,并且仅在调用方的上下文中(销往bar
)销毁.
Then suddenly C
needs to be a fully defined type. But with guaranteed copy elision, none of its members are required. There's no copying or even a move, the value is initialized elsewhere, and destroyed only in the context of the caller (to bar
).
那为什么呢?标准中禁止使用什么?
So why? What in the standard prohibits it?
推荐答案
规则位于 [basic.lval]/9 :
除非另有说明([dcl.type.simple]),否则prvalue必须始终具有完整类型或void类型; ...
Unless otherwise indicated ([dcl.type.simple]), a prvalue shall always have complete type or the void type; ...
这篇关于在保证复制省略的情况下,为什么需要完全定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!