Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 634
Olumide Avatar asked Nov 01 '25 17:11

Olumide


1 Answers

In C++11/14 I would say no, unless you accept to deduce it by passing the pointer as a function argument:

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

int main()
{
    bar(&Foo::func);
}

In C++17 you can have a single parameter function template as shown by @Jarod42, but you don't have the type T deduced anyway (if it was the purpose, as it seems to be from the question).

like image 66
skypjack Avatar answered Nov 03 '25 09:11

skypjack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!