【问题标题】:Template struct specialization for integral/real types整数/实数类型的模板结构特化
【发布时间】:2013-08-21 17:53:00
【问题描述】:

我想为不同类型的数字(整数、浮点数)创建验证器,例如:

typename number_validator<T>::type validator;

我在stdis_integralis_floating_point 中发现了有用的特征。如何使用这些特征来专门化模板 number_validator(它是 struct)?

编辑: 我正在寻找这样的东西:

template<typename T, typename Enabled>
struct number_validator {};

template<typename T>
struct number_validator<T, typename enable_if<is_floating_point<T>::value, T>::type> 
//this doesn't work..
{
    typedef floating_point_validator type;
};

【问题讨论】:

  • 你的“验证者”是做什么的?
  • 它将验证数字。我会将::type 准备为具有公共接口(但不是子类)的某些特定类的类型定义
  • 我要问的是,你想如何验证这些数字?当然还有比这更简单的方法。
  • 可能,但我有点在玩模板;)
  • @Kiel 我的错,稍加修正就可以了。 Here。注意主模板中第二个参数的默认值以及enable_if 的第二个模板参数的缺失。

标签: c++ templates c++11 sfinae


【解决方案1】:

这可能是您正在寻找的,即标签调度

template<typename T, bool = is_integral<T>::value>
struct number_validator {};

template<typename T>
struct number_validator<T, true> 
{
    typedef integral_validator type;
};

template<typename T>
struct number_validator<T, false> 
{
    typedef floating_point_validator type;
};

这假设您真的对数字进行操作,因此类型始终是整数或浮点数。

【讨论】:

    【解决方案2】:

    你可以用整数做到这一点:

    template <typename T, bool isIntegral = std::is_integral<T>::value>
    struct number_validator
    {
       typedef WhatEverType type;
    };
    
    template <typename T>
    struct number_validator<T, true>
    {
        typedef WhatEverTypeInt type;
    };
    

    如果是整数会选择第二个特化,但是如果是浮点数或者其他类型我不知道怎么办。

    【讨论】:

      【解决方案3】:

      在这种情况下,您甚至不需要这些,实际上您可以像这样专门化模板。

      template <typename T>
      struct number_validator;
      template <>
      struct number_validator<float>;
      
      template <>
      struct number_validator<int>;
      
      template <>
      struct number_validator<double>;
      

      这将专门针对每种类型的数字验证器,这要求您列出所有整数和浮点类型。你也可以这样做:

      #include <type_traits>
      #include <iostream>
      template<class T>
      typename std::enable_if<std::is_floating_point<T>::value,T>::type func(T t) { 
          std::cout << "You got a floating point" << std::endl;
          return t;
      }
      
      template<class T>
      typename std::enable_if<std::is_integral<T>::value,T>::type func(T t) { 
          std::cout << "You got an Integral" <<std::endl;
          return t;
      }
      
      int main() {
          float f = func(1.0);
          int a = func(2);
      }
      

      【讨论】:

      • 这实际上可以工作,但你能删除auto(这里是c++03)吗?好吧,您定义了返回值的函数,我更喜欢使用我在问题中编写的代码(作为变量声明),但如果没有使用 auto,您的解决方案就足够了。
      • 我可以删除自动,但你标记了你的问题 c++11 并且 is_integral 是 c++11
      • @Kiel 就是这么简单
      • 哈哈,是的,这很简单。将main 更改为适用于 T 而非任意 int 或 float 的模板函数。请参阅我的问题 - 声明包括 &lt;T&gt; 而不是 int ;)
      • @Kiel 不确定你的要求是什么,你不能模板主
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 2012-01-08
      相关资源
      最近更新 更多