Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in the Standard does it say that the declaration `auto f()() ->int;` is not allowed?

I know I'm being pedantic here, but I'm just trying to understand the C++ grammar production.

I will start with a simple-declaration.

simple-declaration:
    decl-specifier-seqopt   init-declarator-listopt

decl-specifier-seq:
    decl-specifier

decl-specifier:
    type-specifier

type-specifier:
    trailing-type-specifier

trailing-type-specifier:
    simple-type-specifier

simple-type-specifier:
     char
     int
     ...
     auto


Now, I'll look into the definition of a init-declarator-list.

init-declarator-list:
    init-declarator

init-declarator:
    declarator

declarator:
    noptr-declarator   parameters and qualifiers   trailing return type    (*2)

noptr-declarator:
    declarator-id   attribute-specifier-seqopt
    noptr-declarator   parameters and qualifiers    (*1)

parameters and qualifiers:
    (parameter-declaration-clause)   cv-qualifiersopt   ... (all optionals)

trailing-return-type:
    ->   trailing-type-specifier-seq

trailing-type-specifier-seq:
    trailing-type-specifier                See the definition of a traling-type-specifier above.


Replacing noptr-declarator by a declarator-id in (*1), using the previous definition of noptr-declaration, we arrive at the following definition of nonptr-declarator:

noptr-declaration:
    declarator-id   parameters and qualifiers

Now replacing noptr-declarator, parameters and qualifiers and trailing-return-type in (*2) by each definition given above, we obtain the following for declarator:

declarator:
    declarator-id   (parameter-declaration-clause)   (parameter-declaration-clause)   ->
                                    simple-type-specifier


With this last result we could say that the grammar allows the following declaration of a function f:

auto f()() -> int;  

which of course is not valid. But I couldn't find anything in the Standard saying, directly or indirectly, that this construction is ill-formed.

The error messages from GCC (f declared as function returning a function) and clang (auto return without trailing return type; deduced return types are a C++1y extensions) didn't help in this regard either.

like image 752
Ayrosa Avatar asked Dec 01 '22 00:12

Ayrosa


1 Answers

[dcl.fct]/8:

[...] Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. [...]

like image 61
ecatmur Avatar answered Dec 05 '22 09:12

ecatmur