Question

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.

Was it helpful?

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

OTHER TIPS

Yes you can specify parameter names just as with regular function pointers. Keep in mind though these names are only valuable for documenting the function they have no 'real' effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top