Iterating variadic template arguments in reverse order

瘦欲@ 提交于 2019-12-08 05:57:28

问题


The following code is working if I manual reverse the order of the template arguments which is passed to it:

template<typename HeadTag, typename... TailTag>
struct Mapped_scope_deep : public Mapped_scope_deep<TailTag...> {
    typedef typename boost::mpl::at<typename Mapped_scope_deep<TailTag...>::type::type_map,
                                    HeadTag>::type type;
};

template<typename HeadTag>
struct Mapped_scope_deep<HeadTag> {
    typedef typename boost::mpl::at<type_map, HeadTag>::type type;
};

Example:

// typename Mapped_scope_deep<T0, T1, T2, T3>::type
// needs to be written as
typename Mapped_scope_deep<T3, T2, T1, T0>::type

I have tried to fix this here:

template<typename map, typename HeadTag, typename... TailTag>
struct Mapped_scope_deep_r :
    public Mapped_scope_deep_r< typename boost::mpl::at<map, HeadTag>::type::type_map, TailTag...> {
  typename Mapped_scope_deep_r< typename boost::mpl::at<map, HeadTag>::type::type_map, TailTag...>::type type;
};

template<typename map, typename HeadTag>
struct Mapped_scope_deep_r<map, HeadTag> {
    typedef typename boost::mpl::at<map, HeadTag>::type type;
};

template<typename... Tags>
struct Mapped_scope_deep3 :
    public Mapped_scope_deep_r<type_map, Tags...> {
    typedef typename Mapped_scope_deep_r<type_map, Tags...>::type type;
};

Example:

typename Mapped_scope_deep<T0, T1, T2, T3>::type

But this ends in a compile error:

./compressed_enums.hxx:197:66: error: typename specifier refers to non-type member 'type' in 'Gamblify::Asdf<unsigned char, CAT>::Mapped_scope_deep_r<boost::mpl::map<boost::mpl::pair<Cat, Gamblify::Category2<unsigned char, 1, Cat, B_First, TagA_array_2, B_Second> > >, Cat, First>'
    typedef typename Mapped_scope_deep_r<type_map, Tags...>::type type;
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~

What have I done wrong, and is their an easier way to fold-like operations in the reverse order?


回答1:


You're missing a typedef in Mapped_scope_deep_r. This line declares an object, not a type:

typename Mapped_scope_deep_r< typename boost::mpl::at<map, HeadTag>::type::type_map, TailTag...>::type type;

As for reversing the order of a pack, there are some dirty tricks, but the best way is to define a metafunction tuple_reverse and use it to filter the template's input.



来源:https://stackoverflow.com/questions/8773426/iterating-variadic-template-arguments-in-reverse-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!