[英]How to specialize for member function type in template class?
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) 我刚刚测试了以下代码,发现
std::is_function
不接受成员函数类型。(我不确定其他编译器是否正确,我使用的是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? 对于成员函数指针类型,我们可以专门研究此签名:
R (T::*)(Args...)
,但是成员函数类型的对应语法是什么?
it appears, via this link , that the following implmentation is used for is_member_function_pointer 通过此链接 ,似乎将以下实现用于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 TU::* and you can determine if T is a function type. 因此,您可以使用TU :: *来确定某物是否为U类型的某物的成员指针,并且可以确定T是否为函数类型。 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. 但是当我尝试decltype(some_type :: some_member)时,我收到一条错误消息,说我不能只使用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 上述is_member_function_pointer_helper的替代实现可能看起来像
template<class... Args, class R, class U>
struct is_member_function_pointer_helper<R (U::*)(Args...)> : std::true_type {};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.