2015-01-16 36 views
1

如果我有一個類型列表,我怎樣才能得到一個類型的列表,因爲它是可變參數?如何將MPL類型列表摺疊到可變參數容器中?

換句話說,我想從這個去:

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' 

回答

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

輝煌,工作就像一個魅力! –