模板模板参数的参数似乎是非模板类型

时间:2017-04-18 04:43:48

标签: c++ templates template-meta-programming

以下代码会产生错误,但我不确定为何会出现这种情况。

#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;

产生错误:( clang -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)。不应该prepend :: fn是模板类型吗?

1 个答案:

答案 0 :(得分:2)

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

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