gpt4 book ai didi

c++ - 成员函数定义中的不完整类型

转载 作者:IT老高 更新时间:2023-10-28 23:21:36 26 4
gpt4 key购买 nike

[dcl.fct.def] p2状态:

The type of a parameter or the return type for a function definition shall not be an incomplete or abstract (possibly cv-qualified) class type in the context of the function definition unless the function is deleted.

还有 [class.mem] p7状态:

A class is considered a completely-defined object type (or complete type) at the closing } of the class-specifier. The class is regarded as complete within its complete-class contexts; otherwise it is regarded as incomplete within its own class member-specification.

鉴于此代码:

struct S
{
// S is incomplete
S f() { /* S is complete in a function body */ return S(); }
// S is incomplete
};
// S is complete

一个 complete-class context值得注意的是,它不包括函数定义的 decl-specifier-seq,也不包括函数的声明符,但是,每个编译器都说这是可以的。什么措辞允许这样做,因为我找不到它?

最佳答案

引用链接中的第一项:

A complete-class context of a class is a

  • function body ([dcl.fct.def.general]),

所以在任何方法的函数体内都被认为是一个完整的类上下文。据我所知,“函数定义的上下文”是函数体的同义词——与函数声明的上下文相反,函数声明的返回类型不需要完整。

关于c++ - 成员函数定义中的不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57631441/

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