2015-01-16 1 views
1

형식 목록이있는 경우 해당 목록의 형식을 variadic 매개 변수로 가져올 수 있습니까? 즉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 표현을 얻으려면, 당신은 두 개의 클래스 인수를 첫 번째 인수가 types<Ts...> 인의 경우에 add_to_types을 전문으로하는 add_to_types을 변경할 수 있습니다 :

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

화려한이, 마치 마법처럼 일했다! –

관련 문제