2016-02-26 41 views
0

我想根据提供的类型参数来评估一个bitset的值。我现在所拥有的功能是这样的:如何有条件地添加升压MPL?

template <class Set, class PartialSet> 
constexpr auto tupleBitset() { 
    using type = 
     typename mpl::fold< 
      PartialSet, 
      mpl::integral_c<unsigned long long, 0>, 
      mpl::eval_if< 
       mpl::has_key<Set, mpl::_2>, 
       mpl::plus< 
        mpl::_1, 
        mpl::integral_c<unsigned long long, 1ull << getIndex<Set, mpl::_2>()> 
       >, 
       mpl::_1 
      > 
     >::type; 
    return std::bitset<mpl::size<Set>::value>(type::value); 
} 

基本功能的意图的主旨是能够创建一个bitset其位都基于创建的SetPartialSet,这两者都是mpl::set S上的交集。该功能getIndex还提供了:

template <class Set, class Index> 
constexpr auto getIndex() { 
    return mpl::distance< 
      typename mpl::begin<Set>::type, 
      typename mpl::find<Set, Index>::type 
     >::type::value; 
} 

这种做法似乎并不工作,具有编译错误评估到以下几点:

'value' is not a member of 'boost::mpl::same_as<U1> in 'not.hpp' 
'C_': invalid template argument for 'boost::mpl::aux::not_impl', expected compile-time constant expression in not.hpp 

是否有可能左移不是编译时间常量?


看来这个问题是与调用getIndex,因为mpl::_2不被取代。不幸的是,我无法弄清楚如何强制替代。

回答

1

问题是您将place_holder传递给函数getIndex

您可以将您的功能结构类似的

template< typename Set, typename Index > struct getIndex 
    : mpl::integral_c<unsigned long long, (mpl::distance< 
      typename mpl::begin<Set>::type, 
      typename mpl::find<Set, Index>::type 
     >::type::value)> 
{ 
}; 

template <class Set, class PartialSet> 
constexpr auto tupleBitset() { 
    using type = 
     typename mpl::fold< 
      PartialSet, 
      mpl::integral_c<unsigned long long, 0>, 
      mpl::eval_if< 
       mpl::has_key<Set, mpl::_2>, 
       mpl::plus< 
        mpl::_1, 
        mpl::shift_left<mpl::integral_c<unsigned long long, 1ull>, 
            getIndex<Set, mpl::_2>> 
       >, 
       mpl::_1 
      > 
     >::type; 
    return std::bitset<mpl::size<Set>::value>(type::value); 
} 

Demo

+0

这工作,但我不知道你为什么,可以解释为什么它的不同,以它自己的结构使得替代工作? – user975989