The following simple piece of code compiles, although I don't understand why:
class C {
class B;
class A {
B getB() { return B(); }
};
class B {
};
};
int main(int, char**)
{
return 0;
}
If I then comment out the "class C
" stuff, so that the forward declaration of B
, the definition of A
and the definition of B
are no longer nested within a class, the code does not compile, since B
is of an incomplete type:
main.cpp: In member function 'B A::getB()':
main.cpp:6: error: return type 'struct B' is incomplete
main.cpp:6: error: invalid use of incomplete type 'struct B'
main.cpp:3: error: forward declaration of 'struct B'
I understand what it means for a type to be incomplete, namely that it has not been defined yet and so the compiler can't possibly know how much space to allocate for it. But why is B
not considered incomplete in the code above, where A
and B
are both declared and defined inside of C
?
I believe this is a consequence of [basic.scope.class]:
The potential scope of a name declared in a class consists not only of the declarative region following the name’s point of declaration, but also of all function bodies, default arguments, exception-specifications, and brace-or-equal-initializers of non-static data members in that class (including such things in nested classes).
That is, the scope of the full declaration of B
includes the body of the member function of the nested class:
class C {
class B; // (1)
class A {
B getB() {
return B(); // both (1) and (2) in scope here
// since (2) is the complete type declaration,
// this is perfectly fine
}
};
class B { // (2)
};
};
By comparison, if C
were a namespace instead of a class, the scope of the full declaration of class B
would not extend into A::getB()
. The only visible declaration would be the forward-declaration of B
that I labeled (1)
- so B()
would be the construction of an incomplete type there.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加