简体   繁体   中英

Iterating variadic template arguments in reverse order

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?

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.

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