How to deduce class type from method type in C++ templates?

In templates as shown below, I would like the call Run(&Base::foo) succeed without the need to name the Base type twice (as is done in the compiling Run<Base>(&Base::foo) call). Can I have that? Possibly without adding a ton of Boost headers?

With the provided code, I get an error of:

prog.cpp:26: error: no matching function for call to ‘Run(bool (Base::*)())’

(you can fiddle with the snippet at

#include <iostream>

class Base {
  bool foo() { return true; }

Base* x;

template<typename T>
struct Traits {
  typedef bool (T::*BoolMethodPtr)();

template<typename T>
void Run(typename Traits<T>::BoolMethodPtr check) {
  T* y = dynamic_cast<T*>(x);
  std::cout << (y->*check)();

int main() {
  Base y;
  x = &y;
  Run(&Base::foo); // why error?


The T in Traits<T>::BoolMethodPtr is in a non-deduced context, so the compiler will not deduce automatically from the call what type T should be. This is because there could be code like this:

template<typename T>
struct Traits {
  typedef bool (T::*BoolMethodPtr)();

struct Traits<int> {
  typedef bool (Base::*BoolMethodPtr)();

Run(&Base::foo); /* What should T be deduced to? Base and int are both equally possible */

If you can do without the Traits<T> class, you can write Run as:

template<class Class>
void Run(bool (Class::*check)()) {
  Class* y = dynamic_cast<Class*>(x);
  std::cout << (y->*check)();

In this context, Class can be deduced to mean Base

To pick apart a type, any type, use partial specialization. There is no function template partial specialization, so you'll need to directly parameterize the function on its argument type and retrieve the class type inside.

template< typename T >
struct get_host_class; // most types are not ptmfs: don't implement this

template< typename C >
struct get_host_class< bool (C::*)() > { // implement partial specialization
     typedef C host;
     typedef void sfinae; // disallow function for non ptmf arguments

template< typename T >
typename get_host_class<T>::sfinae Run( T check) {
    typedef T BoolMethodPtr; // or something
    typedef typename get_host_class< T >::host host;

I think this is a non deduced context.

$ "The non-deduced contexts are: — The nested-name-specifier of a type that was specified using a qualified-id."

I think this is the quote that applies in this case. But some template gods need to ratify my understanding.

When the compiler tries to match a template argument, it only considers the primary class type. In other words, when it encounters the expression:


...and it's trying to figure out the template parameter for Run, it only considers the type of foo itself, and doesn't consider whatever class foo is a part of.


And the type of foo is bool(Base::*)(void), but what you want the compiler to find is just Base

Need Your Help

WSO2 EMM - Device Name

android wso2-emm

I am testing the WSO2 EMM 2.0.1 product, I would like to know if it is possible to change the name of the mobiles (android) in the EMM console.

Converting to Multi-Threaded Socket Application

c multithreading posix unix

As I am currently doing this project in only C, I've up untill this point only used my webserver as a single threaded application. However, I dont want that anymore! So I have the following code that