以相反的顺序迭代可变参数模板参数 [英] Iterating variadic template arguments in reverse order

查看:186
本文介绍了以相反的顺序迭代可变参数模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我手动颠倒传递给它的模板参数的顺序,下面的代码工作:

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

示例:

 code> // typename Mapped_scope_deep< T0,T1,T2,T3> :: type 
//需要被写为
typename Mapped_scope_deep< T3,T2,T1,T0&

我已尝试在这里修复:

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

示例:

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

结束于编译错误:

  ./ compressed_enums.hxx:197:66:error:typename说明符指非类型成员'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;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ ^ ~~~

我做错了什么,

c>在 Mapped_scope_deep_r 中。此行声明一个对象,而不是类型:

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

至于颠倒一个包的顺序,有一些肮脏的技巧,但最好的方法是定义元函数 tuple_reverse ,并使用它来过滤模板的输入。


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.

这篇关于以相反的顺序迭代可变参数模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆