以相反的顺序迭代可变参数模板参数

时间:2012-01-07 21:56:09

标签: c++ c++11 variadic-templates

如果我手动反转传递给它的模板参数的顺序,则以下代码正常工作:

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

示例:

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

我试图解决这个问题:

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

示例:

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

但是这会以编译错误结束:

./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;
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~

我做错了什么,并且他们以相反的顺序更简单地进行折叠式操作?

1 个答案:

答案 0 :(得分:0)

您错过了typedef中的Mapped_scope_deep_r。该行声明了一个对象,而不是一个类型:

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

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