【问题标题】:C++ boost enable_if questionC++ boost enable_if 问题
【发布时间】:2010-09-26 17:19:54
【问题描述】:

我有什么方法可以简化以下陈述吗? (可能,使用boost::enable_if

我有一个简单的类结构——Base 基类、Derived1Derived2 继承自 Base

我有以下代码:

template <typename Y> struct translator_between<Base, Y> {
   typedef some_translator<Base, Y> type;
};

template <typename Y> struct translator_between<Derived1, Y> {
   typedef some_translator<Derived1, Y> type;
};

template <typename Y> struct translator_between<Derived2, Y> {
   typedef some_translator<Derived2, Y> type;
};

我想使用translator_between 的一个模板特化来编写相同的语句。

我想写的一个例子(伪代码):

template <typename Class, typename Y>

ONLY_INSTANTIATE_THIS_TEMPLATE_IF (Class is 'Base' or any derived from 'Base')

struct translator_between<Class, Y> {
   typedef some_translator<Class, Y> type;
};

有什么方法可以使用boost::enable_ifboost::is_base_of 实现这一点?

【问题讨论】:

    标签: c++ templates boost enable-if


    【解决方案1】:

    首先,您必须在以下选项中做出选择:

    • is_base_of
    • is_convertible

    两者都可以在&lt;boost/type_traits.hpp&gt; 中找到,后者更宽松。

    如果你想简单地阻止这种类型的实例化,那么使用静态断言:

    // C++03
    #include <boost/mpl/assert.hpp>
    
    template <typename From, typename To>
    struct translator_between
    {
      BOOST_MPL_ASSERT((boost::is_base_of<To,From>));
      typedef translator_selector<From,To> type;
    };
    
    // C++0x
    template <typename From, typename To>
    struct translator_between
    {
      static_assert(boost::is_base_of<To,From>::value,
                    "From does not derive from To");
      typedef translator_selector<From,To> type;
    };
    

    由于这里没有发生重载决议,所以您不需要enable_if

    【讨论】:

      【解决方案2】:

      我不认为boost::enable_if 有帮助,因为 SFINAE 似乎更倾向于在函数重载之间进行选择。

      您当然可以使用带有bool 参数的模板来优化选择:

      #include <boost/type_traits.hpp>
      class Base {};
      
      class Derived : public Base {};
      
      template <class A, class B>
      struct some_translator {};
      
      template <typename A, typename B, bool value>
      struct translator_selector;  //perhaps define type as needed
      
      template <typename A, typename B>
      struct translator_selector<A, B, true>
      {
          typedef some_translator<A, B> type;
      };
      
      template <typename A, typename B>
      struct translator_between
      {
          typedef typename translator_selector<A, B, boost::is_base_of<Base, A>::value>::type type;
      };
      
      int main()
      {
          translator_between<Base, int>::type a;
          translator_between<Derived, int>::type b;
          translator_between<float, int>::type c;  //fails
      }
      

      【讨论】:

        【解决方案3】:

        您可以在此处使用 anable_if 和此宏以使其更具可读性:

        #define CLASS_REQUIRES(...) typename boost::enable_if<boost::mpl::and_<__VA_ARGS__, boost::mpl::bool_<true> > >::type
        

        然后你可以这样定义你的类:

        template <typename Class, typename Y, class Enable = 
        CLASS_REQUIRES(boost::is_base_of<Class, Y>)> 
        struct translator_between {
            typedef some_translator<Class, Y> type;
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-19
          • 1970-01-01
          • 2011-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-30
          • 1970-01-01
          相关资源
          最近更新 更多