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最初の引数としてクラス テンプレートが必要です。式を機能させるには、2 つのクラス引数を取り、最初の引数が の場合に特化するようにfold変更できます。add_to_typesadd_to_typestypes<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 に答える