代码之家  ›  专栏  ›  技术社区  ›  Matthieu Brucher

用C++ 17特性替换Boost MPL容器

  •  0
  • Matthieu Brucher  · 技术社区  · 6 年前

    enable_if 要激活这样的调度:

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

    因此,用C++ 17,我可以使用CONTXPR,但我仍然是我必须通过的类型列表。 outside . 有没有合适的方法来声明容器类型以便我可以使用可变模板?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Barry    6 年前

    最简单的转换可能是将Boost.MPL替换为 Boost.MP11 :

    using Types = mp_list<...>;
    
    
    template <typename L>
    void process() {
        if constexpr (!mp_empty<L>) {
            process<mp_pop_front<L>>();   
        }
    }