c++c++11booststd-functionboost-function

Parameter names in a std::function typedef


I know I can typedef a std::function like so:

typedef std::function<void (const std::string&)> TextChangedHandler

Is it permitted to specify parameter names in the typedef, in order to make it more self-documenting? For example:

typedef std::function<void (const std::string& text)> TextChangedHandler

I can add parameter names and it compiles fine on Visual C++ 2010, but I wanted to make sure that it's allowed by the C++03/C++11 standards.


Solution

  • Yes.

    This form of specifying a function type with no declarator naming the function is syntactically a type-id (8.1):

    type-id:
        type-specifier-seq abstract-declarator[opt]
    
    abstract-declarator:
        ptr-operator abstract-declarator[opt]
        direct-abstract-declarator
    
    direct-abstract-declarator:
        direct-abstract-declarator[opt] ( parameter-declaration-clause ) cv-qualifier-seq exception-specification
        direct-abstract-declarator[opt] [ constant-expression ]
        ( abstract-declarator )
    

    The parameter-declaration-clause syntax is the same as in ordinary function declarations, and it allows each argument to either have a name (declarator) or not (abstract-declarator) (8.3.5):

    parameter-declaration-clause:
        parameter-declaration-list[opt] ...[opt]
        parameter-declaration-list[opt] , ...
    
    parameter-declaration-list:
        parameter-declaration
        parameter-declaration-list , parameter-declaration
    
    parameter-declaration:
        decl-specifier-seq declarator
        decl-specifier-seq declarator = assignment-expression
        decl-specifier-seq abstract-declarator[opt]
        decl-specifier-seq abstract-declarator[opt] = assignment-expression