2017-04-18 220 views
2

以下代碼會產生錯誤,但我不確定爲什麼會出現這種情況。模板模板參數的參數似乎是非模板類型

#include <utility> 
#include <type_traits> 
#include <tuple> 

template <typename T, template <typename...> class F, typename = void> 
struct tuple_subtype_apply { 
    using type = typename tuple_subtype_apply<T, F, 
     std::make_index_sequence<std::tuple_size<T>::value>>::type; 
}; 

template <typename T, template <typename...> class F, std::size_t... Is> 
struct tuple_subtype_apply<T, F, std::index_sequence<Is...>> 
{ 
    using type = F<typename std::tuple_element<Is, T>::type...>; 
}; 

template <typename... Args> 
using true_t = std::true_type; 

template <template <typename...> class Fn, typename... Ts> 
struct prepend 
{ 
    template <typename... Args> 
    using fn = Fn<Ts..., Args...>; 
}; 

template <typename T, typename Tup> 
using test = typename tuple_subtype_apply<Tup, 
    prepend<true_t, T>::fn>::type; 

產生的誤差:(鐺-std = C++ 1Z)

<source>:29:24: error: type/value mismatch at argument 2 in template parameter list for 'template<class T, template<class ...> class F, class> struct tuple_subtype_apply' 
    prepend<true_t, T>::fn>::type; 
        ^
<source>:29:24: note: expected a class template, got 'prepend<true_t, T>::fn' 

手動替換模板參數test編譯沒有錯誤(例如typename tuple_subtype_apply<std::tuple<int, int>, prepend<true_t, int>::fn>::type)。不應該預先定義:: fn是模板類型嗎?

回答

1

由於prepend<true_t, T>::fndependent name,因此需要在fn之前添加template

template <typename T, typename Tup> 
using test = typename tuple_subtype_apply<Tup, 
    prepend<true_t, T>::template fn>::type;