我可以重载类型特征的函数吗? [英] Can I overload functions with type-traits?

查看:232
本文介绍了我可以重载类型特征的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有六种类型,并且它们都属于一个概念类别。

这是一个图表显示:








或者可能是一个更具体的例子:






我想编写两个函数来处理所有6种类型。

类别1中的类型得到处理某种方式,类型在类别2得到处理不同的方式。



让我们进入代码。
首先,我将创建六种类型。

  //类别1类型
class Type_A { };
class Type_B {};
class Type_C {};

//类别2类型
class Type_D {};
class TypeEE {};
class Type_F {};

接下来,我将创建两个类型traits,以便可以在编译时发现类型的类别时间。

  / *构建类别1类型特性* / 

// Type_A Type Trait
template< typename T>
struct Is_Type_A {
static const bool value = false;
};
模板<>
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;
};
模板<>
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;
};
模板<>
struct Is_Type_C< Type_C> {
static const bool value = true;
};

//类别1类型Trait
模板< typename T>
struct Is_Type_From_Category_1 {
static const bool value = Is_Type_A< T> :: value || Is_Type_B< T> :: value || value_value =< T>
};

/ *构建类别2类型特性* /

// Type_D Type Trait
template< typename T>
struct Is_Type_D {
static const bool value = false;
};
模板<>
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;
};
模板<>
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;
};
模板<>
struct Is_Type_F< Type_F> {
static const bool value = true;
};

//类别1类型Trait
模板< 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,但这样的尝试会导致

  //处理类别1中的所有类型
template< class T,class = typename std :: enable_if< Is_Type_From_Category_1< T> :: value> :: type>
void function(T t){
//对类型1做类别1的东西
return;
}

//处理类别2中的所有类型
template< class T,class = typename std :: enable_if< Is_Type_From_Category_2< T> :: value> :: type> ;
void function(T t){
//做类别2的东西到类型
return;
}






编辑2 :我尝试过链接中提供的代码,但这不是一个是否决定是否调用该函数。这是我调用哪个函数,给定两个类型特征。这将是一个重定义错误。

  //处理类别2中的所有类型
template< class T, = typename std :: enable_if< Is_Type_From_Category_1< T> :: value,void> :: type>
void function(T t){
//做类别1的东西到类型
return;
}
//从类别2处理所有类型
template< class T,class dummy = typename std :: enable_if< Is_Type_From_Category_2< T> :: value,void> :: type>
void function(T t){
//做类别2的东西到类型
return;
}


解决方案

仅与模板参数的默认值不同。如果你明确调用 function< int,void>



通常使用 enable_if 类型

  //处理类别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;
}

//从类别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;
}


Let's say, I have six types, and they each belong in a conceptual category.
Here is a diagram that shows this:


Or Perhaps a more specific example for you:


I want to write two functions that will handle all 6 types.
Types in "Category 1" get handled a certain way, and types in "Category 2" get handled a different way.

Let's get into the code. First, I'll create the six types.

//Category 1 Types
class Type_A{};
class Type_B{};
class Type_C{};

//Category 2 Types
class Type_D{};
class Type_E{};
class Type_F{};

Next, I'll create two type traits so that the category of the type can be discovered at compile time.

/* 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;
};

Now that I have two type traits to distinguish what category each of the six types fall into, I want to write two functions. One function will accept everything from Category 1, and the other function will accept everything from Category 2. Is there a way to do this without creating some kind of dispatching function? Can I find a way to have only two functions; one for each category?


EDIT: I have tried to use enable_if like this, but such an attempt will result in a compiler error.

//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;
}


Edit 2: I've tried the code provided in the link, but this isn't a yes or no decision on whether or not to call the function. It's which function do I call, given two type traits. This would be a redefinition error.

//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;
}

解决方案

Two functions signatures are not allowed to differ only by the default value of a template parameter. What would happen if you explicitly called function< int, void >?

Usual usage of enable_if is as the function return type.

//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;
}

这篇关于我可以重载类型特征的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆