Why forward declaration is allowed inside function argument list?

  • A+
Category:Languages

This code shouldn't compile in my opinion, but it seems compiler treats struct NonExistingNeverDeclaredType* argument as a forward declaration (proof). But why?

#include <iostream>  int foo(struct NonExistingNeverDeclaredType* arg) {   return sizeof(arg); }  int main() {   std::cout << foo(nullptr) << std::endl;   return 0; } 

https://ideone.com/v3XzCw

 


This is a property of the elaborated type specifier, it will introduce a declaration if the type is not previously declared:

cppreference

If the name lookup does not find a previously declared type name, the elaborated-type-specifier is introduced by class, struct, or union (i.e. not by enum), and class-name is an unqualified identifier, then the elaborated-type-specifier is a class declaration of the class-name.

It doesn't matter whether it's inside a function argument list or not. The following code is also valid:

class foo {     class bar *b; // bar is not previously declared }; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: