Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class declaration within a function declaration

Consider the following example:

#include <iostream>

void foo(class B, B *b); 

B *c; //OK

int main(){ }

DEMO

The Standard N4296::3.3.2/7.1 [basic.scope.pdecl]

— for a declaration of the form

class-key attribute-specifier-seqopt identifier;

the identifier is declared to be a class-name in the scope that contains the declaration

, but according to N4296:3.3.4/1 [basic.scope.proto]

In a function declaration, or in any function declarator except the declarator of a function definition (8.4), names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearest enclosing function declarator.

So, the class B should have been introduced in the function prototype scope. And the scope of B should have been up to the end of the foo's declarator. But the name is visible in the global scope. Why?


1 Answers

You are quoting the wrong bullet in 3.3.2 [basic.scope.pdecl]/p7. The class B in the declaration of foo is not of the form class-key attribute-specifier-seq_opt identifier; - there is no semicolon.

Instead, the second bullet applies:

for an elaborated-type-specifier of the form

class-key identifier

if the elaborated-type-specifier is used in the decl-specifier-seq or parameter-declaration-clause of a function defined in namespace scope, the identifier is declared as a class-name in the namespace that contains the declaration; otherwise, except as a friend declaration, the identifier is declared in the smallest namespace or block scope that contains the declaration.

Hence, the elaborated-type-specifier class B in your example declares B as a class-name in the namespace containing the declaration of foo - i.e., the global namespace.

like image 69
T.C. Avatar answered Feb 28 '26 23:02

T.C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!