c ++:模板声明中is_member_function_pointer的语法 [英] c++ : syntax for is_member_function_pointer in a template declaration
问题描述
我有一个类似这样声明的模板:
I have a template with a declaration similar to this:
template <typename Arg0, typename... Args>
class blah {};
我有两个版本的模板,当Arg0是一个成员函数指针,否则使用另一个。我试图使用std :: enable_if和std :: is_member_function_pointer,但我找不到正确的语法。这是我对真实情况:
I have two versions of the template, and I want to use one when Arg0 is a member function pointer, otherwise use the other one. I'm trying to use std::enable_if and std::is_member_function_pointer but I cannot find the correct syntax. This is what I have for the true case:
template<typename = typename std::enable_if< std::is_member_function_pointer<Arg0> >::type, typename... Args>
class blah() {}
但这显然不是句法上正确的。
But this obviously isn't syntactically correct.
推荐答案
在类中使用布尔谓词时,通常有两种方法可供选择:
When using the Boolean predicates with classes there are generally two approaches I use to make the choice:
-
如果我只需要在两种类型之间选择,我使用sonething像
If I just need to choose between two types, I use sonething like
typename std::conditional<
std::is_member_function_pointer<F>::value,
type_when_true, type_when_false>::type
如果事情需要改变,我从一个专门的布尔值覆盖两个实现选择的基础派生:
If things need to change more than that I derive from a base which is specialized on a Boolean covering the two implementation choices:
template <bool, typename...>
struct helper;
template <typename... A>
struct helper<true, A...> {
// implementation 1
};
template <typename... A>
struct helper<false, A...> {
// the other 1
};
template <typename F, typename... A>
struct actual
: helper<std::is_member_function_pointer<F>::value, F, A...>
{
// typedefs, using ctors, common code, etc.
};
这篇关于c ++:模板声明中is_member_function_pointer的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!