【问题标题】:Looking for a metafunction from bool to bool_type [closed]寻找从 bool 到 bool_type 的元函数 [关闭]
【发布时间】:2010-01-22 22:33:32
【问题描述】:

基本上,我正在寻找这样的库解决方案:

#include <boost/type_traits.hpp>

template<bool>
struct bool_to_bool_type;

template<>
struct bool_to_bool_type<false>
{
    typedef boost::false_type type;
};

template<>
struct bool_to_bool_type<true>
{
    typedef boost::true_type type;
};

有这样的元函数吗?

【问题讨论】:

  • 你想用这个达到什么目的?
  • @AraK:创建新类型特征时,您需要知道这一点。我做了几个。此外,有时我们只是希望将常量转换为类型,以选择不同的功能。
  • @GMan 谢谢。元编程是我的眼睛在 C++ 中看不到的阴暗面 :)
  • @AraK:有朝一日,纯净之光可以照进广阔的 C++,让你看到真相。

标签: c++ boost metaprogramming


【解决方案1】:

提升already providesboost::mpl::bool_&lt;&gt;:

Boost.TypeTraits 使用integral_constant:

BOOST_STATIC_ASSERT(( 
    is_same< integral_constant<bool, false>, 
             is_class<int>::type >::value ));

【讨论】:

    【解决方案2】:

    哦等等,true_type 只是 std::integral_constant&lt;bool, true&gt; 的 typedef?然后有一个明显的解决方案:

    boost::integral_constant<bool, input_value>
    

    【讨论】:

    • 就是这样。 :) 虽然你的意思是boost::integral_constant,而不是std::
    • 是的,我总是混淆 booststdstd::tr1 命名空间... :)
    • Fred,integral_constant 是 - 这就是我在回答中试图表达的意思。也许您可以先阅读答案? :p
    【解决方案3】:

    虽然integral_constant 是答案,但您可以创建一两个类型以使事情更清楚一些。我在我的图书馆中使用它:

    // utility/bool_type.hpp
    #include <boost/type_traits/integral_constant.hpp>
    
    namespace utility
    {
        template <bool B>
        struct bool_type : boost::integral_constant<bool, B>
        {
            static const bool value = B;
        };
    
        typedef const boost::true_type& true_tag;
        typedef const boost::false_type& false_tag;
    }
    
    // main.cpp
    // just some predicate for example
    template <typename T>
    struct is_pointer
    {
        static const bool value = false;
    };
    
    template <typename T>
    struct is_pointer<T*>
    {
        static const bool value = true;
    };
    
    // some specialized function.
    // the true/false tag parameter is a
    // little easier to read, i think
    template <typename T>
    void foo(T, utility::true_tag)
    {
    }
    
    /* versus:
    template <typename T>
    void foo(T, const boost::true_type&)
    */
    
    template <typename T>
    void foo(T, utility::false_tag)
    {
    }
    
    // the actual function
    template <typename T>
    void foo(T pX)
    {
        // a bit shorter
        foo(pX, utility::bool_type<is_pointer<T>::value>());
    
        /* versus:
        foo(pX, boost::integral_type<bool, is_pointer<T>::value>());
        */
    }
    
    int main(void)
    {
        int i = 0;
        foo(i);
        foo(&i);
    }
    

    但这只是我的意见。 :]

    【讨论】:

      【解决方案4】:

      GMan 有一个更好的方法来做你正在做的事情,只需让你的谓词元函数派生自 true/false_type(那些是 integer_constant 的类型别名),例如:

      #include <type_traits>
      
      template <typename T>
      struct is_pointer : std::false_type {};
      
      template <typename T>
      struct is_pointer<T*> : std::true_type {};
      
      //....
      foo(px, is_pointer<T>());
      

      注意:我使用的是 C++0x 库。

      当你有像integral_type这样的元数据包装器时,bool->bool_type元函数的整个想法是多余的,只要仔细看看它的定义,看看我在说什么,它的设计就像我的例子。

      【讨论】:

      • 恐怕你错过了我的帖子的重点。我的谓词只是举例。正如 OP 所展示的,bool 本身甚至可能不是来自这样的谓词。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多