c++metaprogrammingc++17boost-mpl

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?


Solution

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