2

如果我有一个类型列表,我怎样才能获得具有该列表的类型,因为它是可变参数?

换句话说,我想从这个出发:

boost::mpl::list<foo, bar, baz, quux>

至:

types<foo, bar, baz, quux>

(顺序不重要)

这是我使用的尝试fold

typedef boost::mpl::list<foo, bar, baz, quux> type_list;

template <typename... Ts>
struct types {};

template <template <typename... Ts> class List, typename T>
struct add_to_types {
  typedef types<T, typename Ts...> type;
};

typedef boost::mpl::fold<
  type_list,
  types<>,
  add_to_types<boost::mpl::_1, boost::mpl::_2>
>::type final_type;

不幸的是,这给了我关于占位符的错误:

error: type/value mismatch at argument 1 in template parameter list for 'template<template<class ... Ts> class List, class T> struct add_to_types'
error:   expected a class template, got 'mpl_::_1 {aka mpl_::arg<1>}'
error: template argument 3 is invalid
error: expected initializer before 'final_type'
4

1 回答 1

3

问题是这types<Ts...>是一个类而不是类模板。但是,您add_to_types希望将类模板作为第一个参数。为了使您的fold表达式起作用,您可以更改add_to_types为采用两个类参数并专门add_to_types针对第一个参数的情况types<Ts...>

template <typename Seq, typename T>
struct add_to_types;

template <typename T, typename... Ts>
struct add_to_types<types<Ts...>, T>
{
  typedef types<T, Ts...> type;
};
于 2015-01-16T14:16:15.710 回答