TransWikia.com

why a class type is considers as complete type when the its name appears as the return type of a member function that in the definition of such class

Stack Overflow Asked by jack X on July 24, 2020

struct Test{
  Test show(){
    return Test{};
  }
  int a;
};

Consider the above code , The return type of a function shall be a complete type, because of this:
dcl.fct#11

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

The type Test is used as the return type of function show, which is defined within the definition of class Test. However according to the standard, the class type Test is considered as incomplete type at that point, because of this:
class.mem#6

A class is considered a completely-defined object type ([basic.types]) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, noexcept-specifiers, and default member initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

The member function show is not deleted, and it is defined within the definition of class Test. The return type satisfies none of the quote says what as I have emphasized for. At this point, the return type shall be an incomplete type. However the compiler consider the code as well-formed

As a opposite:

struct B;
struct Test{
  B show(){
  }
  int a;
};

the compiler will give an explicitly error
notice that denotes the return type B is incomplete. It’s in accordance with the rules.

why compilers consider the first example as a well-formed code? It it a bug? If I miss anything special rule regarding such case, please point it out.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP