lundi 27 juillet 2015

Template argument deduction in alias templates

While answering a question, I proposed utilizing template aliases for typedefing the signature of a member function; that is, not just typedefing a member function but being able to factor out the target class that contains the method:

template<typename T>
using memberf_pointer = int (T::*)(int, int); 

While the above seems to cover what the question asked, I tried to generalize it for arbitrary function arguments:

template<typename T, typename... Args>
using memberf_pointer = int (T::*)(Args&&...); 

It fails with argument deduction issues (basically it assumes an empty arument list). Here's a demo:

#include <iostream>

class foo
{
public:
  int g (int x, int y) { return x + y ; }
};

template<typename T, typename...Args>
using memberf_pointer = int (T::*)(Args&&...); 

int main()
{
  foo f ;
  memberf_pointer<foo> mp = &foo::g ;
  std::cout << (f.*mp) (5, 8) << std::endl ;
}

Why is this? Is there a way to get it to work?

Aucun commentaire:

Enregistrer un commentaire