2

我刚刚测试了以下代码,我发现std::is_function不接受成员函数的类型。(我不知道这是不是真的为其他编译器,我使用MVC++ 2012年11月CTP)如何专注于模板类中的成员函数类型?

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

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

所以我想实现它:

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 

对于一个成员函数指针类型,我们可以专门为这个签名:R (T::*)(Args...) ,但什么是一个成员函数类型相应的语法?

+0

这有什么错['标准:: is_member_function_pointer'(http://en.cppreference.com/w/cpp/types/is_member_function_pointer) ? –

+0

在C++中没有成员函数数据类型,只有成员函数指针。 –

+0

'decltype(Ac :: af)'不合格 - 在编译器的上面代码输出'0'?啊,visual-studio-2012。我看到你的问题!据我所知,它是非法的,以非常静态的方式谈论非静态成员函数,而不需要通过'(blah blah blah)'调用来调用它并跟踪它,或者用一个'&'。 – Yakk

回答

0

看来,via this link,下列implmentation用于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 
            > {}; 

因此可以判断,如果事情是什么东西类型的ü使用TU :: *,你可以判断一个成员指针T是一种功能类型。我不知道任何成员函数类型的语法,只有成员函数指针类型。我不得不咨询标准来看看这种类型是否可以存在。

如果这种情况不存在,你可以实现一个包装类,它为你添加指针。

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

但是当我尝试decltype(some_type :: some_member)我得到一个错误,说我不能只用some_type :: some_member。一个“&”需要

的函数成员指针下面的作品

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

在我看来,你只能使用成员指针,而不仅仅是成员类型。

的替代实现上述is_member_function_pointer_helper的可能看起来像

template<class... Args, class R, class U> 
struct is_member_function_pointer_helper<R (U::*)(Args...)> : std::true_type {}; 
相关问题