Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases for function type typedefs with qualifiers

Tags:

c++

One can typedef function types, even with class cv- and ref-qualifiers:

typedef void F() const volatile &&;

This can be used to declare (but not define) non-static member functions:

struct A {
    F f;    // void f() const volatile &&
};

This use for F is explicitly forbidden in dependent contexts in templates.

It can also be used as template argument. As far as I can tell, there is however no way of extracting or manipulating the cv- and ref-qualifiers of the type.

Is there any other use case for such a typedef (with any qualifier, not necessarily all of them at once)?

like image 627
walnut Avatar asked Sep 25 '19 09:09

walnut


1 Answers

The standard specifies the only allowed use cases of such function types (function types with a cv-qualifier-seq or a ref-qualifier) in [dcl.fct]/6:

A function type with a cv-qualifier-seq or a ref-qualifier (including a type named by typedef-name ([dcl.typedef], [temp.param])) shall appear only as:

  • (6.1) the function type for a non-static member function,

  • (6.2) the function type to which a pointer to member refers,

  • (6.3) the top-level function type of a function typedef declaration or alias-declaration,

  • (6.4) the type-id in the default argument of a type-parameter, or

  • (6.5) the type-id of a template-argument for a type-parameter ([temp.arg.type]).

Minimal reproducible representative example (mre):

using Func = void() const;
using Func2 = Func;       // (6.3)

struct C {
    Func f;               // (6.1)
};

Func C::* ptr;            // (6.2)

template <class T = Func> // (6.4)
struct S { };

S<Func> x;                // (6.5)
like image 128
L. F. Avatar answered Nov 14 '22 23:11

L. F.