Type of the first parameter of a member function in C ++ 11

I wrote a metafunction to get the type of the first parameter of a member function, which, of course, receives one or more parameters. The code I wrote is as follows:

template <typename...> struct parameter; template < typename O, typename A, typename R, typename... Args> struct parameter <R (O::*)(A, Args...) > { using first_param = A; }; 

I use this meta function as follows:

 using mem_fn = void(mem_type::*)(std::vector<int>); using f_pm = parameter<mem_fn>::first_param; 

and it compiles and works. But when I have a class:

 struct mem_type{ void update(std::vector<int>) { } }; 

and use my metaphor as follows:

 using mem_fn = decltype(mem_type::update); using f_pm = parameter<mem_fn>::first_param; 

the code does not compile, but visual studio 2013 gives: error C2027: using undefined type parameter<mem_fn> .

Does anyone know the cause of this error?

+7
c ++ c ++ 11 templates template-meta-programming
source share
1 answer

First, an id expression that names a non-static member function ( mem_type::update in this case) cannot be used as an unvalued operand (such as the decltype operand). ยง5.1.1 [expr.prim.general] / p13 (footnote omitted):

An identifier that denotes a non-static data element or non-static function of a class member can be used only:

  • as part of access to a member of a class (5.2.5), in which an object expression refers to a class of participants or a class derived from that class or
  • to form a pointer to an element (5.3.1) or
  • if this id expression denotes a non-static data element and it appears in an unvalued operand.

ยง7.1.6.2 [dcl.type.simple] / p4:

The decltype specifier operand is an decltype operand (Clause 5).

And even if update was a regular function, decltype would create a function type, not a function pointer type, and your specialization would correspond to a member pointer type.

You need to create a member pointer function using & - ie, decltype(&mem_type::update) .

+4
source share

All Articles