【发布时间】:2014-01-09 01:08:41
【问题描述】:
假设我有六种类型,它们都属于一个概念类别。
这是一个显示这一点的图表:
或者也许为您提供更具体的示例:
我想编写两个函数来处理所有 6 种类型。
“类别 1”中的类型以特定方式处理,而“类别 2”中的类型以不同方式处理。
让我们进入代码。 首先,我将创建六种类型。
//Category 1 Types
class Type_A{};
class Type_B{};
class Type_C{};
//Category 2 Types
class Type_D{};
class Type_E{};
class Type_F{};
接下来,我将创建两个类型特征,以便可以在编译时发现类型的类别。
/* Build The Category 1 Type Trait */
//Type_A Type Trait
template <typename T>
struct Is_Type_A {
static const bool value = false;
};
template <>
struct Is_Type_A<Type_A> {
static const bool value = true;
};
//Type_B Type Trait
template <typename T>
struct Is_Type_B {
static const bool value = false;
};
template <>
struct Is_Type_B<Type_B> {
static const bool value = true;
};
//Type_C Type Trait
template <typename T>
struct Is_Type_C {
static const bool value = false;
};
template <>
struct Is_Type_C<Type_C> {
static const bool value = true;
};
//Category 1 Type Trait
template <typename T>
struct Is_Type_From_Category_1 {
static const bool value = Is_Type_A<T>::value || Is_Type_B<T>::value || Is_Type_C<T>::value;
};
/* Build The Category 2 Type Trait */
//Type_D Type Trait
template <typename T>
struct Is_Type_D {
static const bool value = false;
};
template <>
struct Is_Type_D<Type_D> {
static const bool value = true;
};
//Type_E Type Trait
template <typename T>
struct Is_Type_E {
static const bool value = false;
};
template <>
struct Is_Type_E<Type_E> {
static const bool value = true;
};
//Type_F Type Trait
template <typename T>
struct Is_Type_F {
static const bool value = false;
};
template <>
struct Is_Type_F<Type_F> {
static const bool value = true;
};
//Category 1 Type Trait
template <typename T>
struct Is_Type_From_Category_2 {
static const bool value = Is_Type_D<T>::value || Is_Type_E<T>::value || Is_Type_F<T>::value;
};
现在我有两个类型特征来区分这六种类型中的每一种属于哪个类别,我想编写两个函数。一个函数将接受类别 1 中的所有内容,而另一个函数将接受类别 2 中的所有内容。有没有办法在不创建某种调度函数的情况下做到这一点?我能找到一种只有两个功能的方法吗?每个类别一个?
编辑:我曾尝试像这样使用 enable_if,但这样的尝试会导致编译器错误。
//Handle all types from Category 1
template<class T ,class = typename std::enable_if<Is_Type_From_Category_1<T>::value>::type >
void function(T t){
//do category 1 stuff to the type
return;
}
//Handle all types from Category 2
template<class T ,class = typename std::enable_if<Is_Type_From_Category_2<T>::value>::type >
void function(T t){
//do category 2 stuff to the type
return;
}
编辑 2: 我已经尝试了链接中提供的代码,但这并不是关于是否调用该函数的是或否决定。考虑到两种类型特征,我应该调用哪个函数。这将是一个重新定义错误。
//Handle all types from Category 2
template<class T, class dummy = typename std::enable_if< Is_Type_From_Category_1<T>::value, void>::type>
void function(T t){
//do category 1 stuff to the type
return;
}
//Handle all types from Category 2
template<class T, class dummy = typename std::enable_if< Is_Type_From_Category_2<T>::value, void>::type>
void function(T t){
//do category 2 stuff to the type
return;
}
【问题讨论】:
-
Understanding SFINAE的可能重复
-
@Potatoswatter 对,但我认为我不能用类型特征重载,可以吗?必须先发现它们是真是假;这意味着我必须派遣?
-
查看链接的问答。
enable_if不是您要找的吗? -
@Potatoswatter 很好,我已经尝试过 enable_if,但我无法编写两个使用 enable_if 的相同函数签名;至少我不这么认为。我将尝试更新我的问题。
-
stackoverflow.com/questions/15427667/… 我认为您可以接受一个不错的简短答案
标签: c++ templates c++11 template-meta-programming typetraits