c++ - 我可以重载具有类型特征的函数吗?

标签 c++ templates c++11 template-meta-programming typetraits

假设我有六种类型,它们都属于一个概念类别。
这是一个显示这一点的图表:

Types A, B, and C wrapped inside a box called "Type Category 1" and types D, E, and F wrapped inside a box called "Type Category 2"


或者也许为您提供更具体的示例: Apple, Orange and Banana are all Fruit.  Carrot, Onion, and Cabbage are all Vegetables


我想写两个函数来处理所有 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;
}

最佳答案

不允许两个函数签名仅因模板参数的默认值而有所不同。如果你明确调用 function< int, void > 会发生什么? ?

enable_if 的常用用法是作为函数返回类型。

//Handle all types from Category 1
template<class T >
typename std::enable_if<Is_Type_From_Category_1<T>::value>::type
function(T t){
    //do category 1 stuff to the type
    return;
}

//Handle all types from Category 2
template<class T >
typename std::enable_if<Is_Type_From_Category_2<T>::value>::type
function(T t){
    //do category 2 stuff to the type
    return;
}

关于c++ - 我可以重载具有类型特征的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20699081/

相关文章:

C++ 3D vector 模板

javascript - Emscripten:如何捕获JS异常?

javascript - SpiderMonkey 是线程安全的是什么意思?

c++ - 是否可以声明一个 constexpr 指针而不是指向 constexpr 的指针?

c++ - 通用 vector 类实现

c++ - 头文件和 odr 中的 constexpr 全局常量

c++ - C++ 中的函数可以返回一些东西然后执行它的代码吗?

c++ - 需要模板类和理解行错误的指导

c++ - 调用 std::functions

c++ - 是否应该将运算符声明为非成员非模板 friend