Search code examples
c++templatespointer-to-membertype-deductiontemplate-argument-deduction

Can class type be deduced from pointer to member function template parameter


Is it possible to deduce the type of the class T from its pointer to memmber T::*f as shown below.

struct Foo
{
    void func(){}
};

template<typename T, void (T::*f)()>
void bar()
{
}

int main()
{
    bar<Foo,Foo::func>();
    // bar<Foo::func>(); // Desired
}

Solution

  • In C++17, you will be allowed to write

    template<auto M>
    void bar();
    

    Which allows

    bar<&Foo::func>();