如何有条件地添加增强 MPL

How do I conditionally add with boost MPL?

本文关键字:增强 MPL 添加 有条件      更新时间:2023-10-16

我正在尝试根据提供的类型参数评估位集的值。我现在的功能是这样的:

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

基本上,该函数意图的要点是能够创建一个位集,其位是基于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没有被替换。不幸的是,我不知道如何强制替换。

问题是您将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);
}

演示