Do you think that the initialized declarator is a valid lexical structure inside of the caught-declaration part of the catch statement? For example, take a look at the following code:
void func( int = 1 )
{
try
{
}
catch( int a = 1 )
{
}
}
It compiles fine under latest MSVC 17.0.2 but fails to compile under latest GCC 11.2 (tested using Godbolt.org). I want to know the answer to form a purely lexical understanding about the correct typing of C code.
If you read this cppreference.com article then you find that it says that the declaration should be exactly the same as(*) for function signature arguments, thus putting legitimacy into the MSVC C lexer.
* It's not actually the same. The text just happens to distinguish between just a declarator and the initializer part being separate.
CodePudding user response:
No. It's invalid.
The grammar for the catch clause specifies type-specifier-seq declarator
. The declarator
part of that does not include an initializer. Compare this with the grammar for a function parameter, which does allow an initializer:
attr(optional) decl-specifier-seq declarator = initializer
CodePudding user response:
first: No the article does not says it allows initializer in catch statement.
It says to refer to the function specification for
type-specifier-seq
declarator
abstract-declarator
or the initializer
is a different element.
Second:
An initializer in a catch statement is pointless since a value is required for actualy going into a catch statement. which means that the initializer value would never be used, and it would jeopardize the program flow since the initalization could raise an exception that will at best terminate.
MSVC probably tolerate it out of pointlessness since it cannot affect the program