简体   繁体   中英

Replacing Boost MPL containers with C++17 features

I have some old code based on MPL containers, using enable_if to to activate some dispatch like this:

typedef boost::mpl::vector<std::int16_t, std::int32_t, int64_t, float, double, std::complex<float>, std::complex<double> > Types;

template <typename Vector>
typename boost::enable_if<typename boost::mpl::empty<Vector>::type, void>::type void process(/*args*/)
{
}

template <typename Vector>
typename boost::disable_if<typename boost::mpl::empty<Vector>::type, void>::type void process(/*args*/)
{
    process<typename boost::mpl::pop_front<Vector>::type>();
}

void outside()
{
    process<Types>();
}

So with C++17, I can use constexpr, but I still that list of types that I have to pass in outside . Is there a proper way of declaring the container type so that I can use variadic templates?

Probably the easiest transition would be to swap Boost.MPL for Boost.MP11 :

using Types = mp_list<...>;


template <typename L>
void process() {
    if constexpr (!mp_empty<L>) {
        process<mp_pop_front<L>>();   
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM