Question

I just tested the following codes, and I find out std::is_function doesn't accept member function types.(I'm not sure if this is true for other compilers, I'm using MVC++ Nov 2012 CTP)

   class Ac {
    public:
      float af(int) {}
    };

    int main() {
     std::cout <<  std::is_function<decltype(Ac::af)>::value << '\n'; //output is 0
    }

so I'm trying to implement it:

template<typename T>
struct is_member_function : std::false_type {};
template<typename T, typename R, typename... Args>
struct is_member_function<R (T::) (Args...)> : std::true_type {}; //this doesn't compile

For a member function pointer type, we can specialize for this signature: R (T::*)(Args...) , but what is the corresponding syntax for a member function type?

Was it helpful?

Solution

it appears, via this link, that the following implmentation is used for is_member_function_pointer

template< class T >
struct is_member_function_pointer_helper : std::false_type {};

template< class T, class U> 
struct is_member_function_pointer_helper<T U::*> : std::is_function<T> {};

template< class T >
struct is_member_function_pointer : is_member_function_pointer_helper<
                                        typename std::remove_cv<T>::type
                                    > {};

So you can determine if something is a member pointer for something of type U using the T U::* and you can determine if T is a function type. I don't know of any syntax for a member function type, only a member function pointer type. I'd have to consult the standard to see if such a type can exist.

If it is the case that this does not exist you could implement a wrapper class which added the pointer for you like so.

template<class T>
struct is_member_function {
    static const bool value = std::is_member_function_pointer<T*>::value;
};

But when I try decltype(some_type::some_member) I get an error saying I can't just use some_type::some_member. a '&' is required

The following works for function member pointers

std::is_member_function_pointer<decltype(&foo::hello)>::value

It seems to me that you can only use member pointers and not just member types.

an alternate implementation of the above is_member_function_pointer_helper might look like

template<class... Args, class R, class U>
struct is_member_function_pointer_helper<R (U::*)(Args...)> : std::true_type {};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top