38

我可以使用模板别名作为模板模板参数吗?

template <template <typename...> class> struct foo {};

template <typename T> using simple_ptr = std::unique_ptr<T>;

foo<std::unique_ptr> a; // this doesn't work, std::unique_ptr has two parameters
foo<simple_ptr> b; // does this work?
4

2 回答 2

26

是的,这显然是允许的。根据我能找到的即将出台的标准的最新草案,据说

模板模板参数的模板参数应该是类模板的名称或别名模板[...]。

但是,目前似乎很少支持别名模板,因此在大多数编译器中使用它可能会遇到一些麻烦。

于 2011-09-06T12:35:55.370 回答
1

阅读原始问题的人可能正在编写使用模板模板参数作为元函数的结构,如下面的清单所示。

template <int T>
struct integer
{
        using value = T;
};

template <class T, class U, template <class...> class Function>
struct binary_op
{
        // Works for add_1, but not add_2
        using type = typename Function<T, U>::type;

        // Works for add_2, but not add_1
        using type = Function<T, U>;
};

template <class T, class U>
struct add_1;

template <int T, int U>
struct add_1<integer<T>, integer<U>>
{
        using type = integer<T + U>;
};

template <class T, class U>
using add_2 = typename add_1<T, U>::type;

add_1并且add_2都是元函数,让我们区分一下

  • add_1作为嵌套 typedef 样式元函数的示例(c++03 支持)
  • add_2作为模板别名样式元函数的示例(需要 c++11)

binary_op结构可以与模板别名样式嵌套的 typedef 样式元函数一起使用,但不能同时使用两者。在这个答案中,我展示了如何重写这样的 TMP 代码来避免这个问题。

假设您希望将模板模板参数Function应用于值参数包Ts...。要应用元功能,您需要

using type = Function<Ts...>; // template-alias style

或者

using type = typename Function<Ts...>::type; // nested typedef style

拥有另一个通用元函数来检测传递的元函数类型并相应地应用它会很有用。

下面is_alias_metafunction实现的功能是此类设施的构建块:

#include <type_traits>

template <class... Ts>
struct sequence;

template <class T>
struct check
{
    static constexpr bool value = true;
};

template <
    template <class...> class Function,
    class                     S,
    class                     Check = void
>
struct is_alias_metafunction
{
    static constexpr bool value = true;
};

template <
    template <class...> class Function,
    class...                  Ts
>
struct is_alias_metafunction<
    Function,
    sequence<Ts...>,
    typename std::enable_if<
        check<typename Function<Ts...>::type>::value
    >::type
>
{
    static constexpr bool value = false;
};

现在,我们可以编写一个元函数apply,将模板模板参数Function应用于参数包Ts...,无论Function是模板别名还是模板结构。

template <
    bool                      IsAlias,
    template <class...> class Function,
    class                     S
>
struct apply_impl;

template <template <class...> class Function, class... Ts>
struct apply_impl<true, Function, sequence<Ts...>>
{
    using type = Function<Ts...>;
};

template <template <class...> class Function, class... Ts>
struct apply_impl<false, Function, sequence<Ts...>>
{
    using type = typename Function<Ts...>::type;
};

template <template <class...> class Function, class... Ts>
using apply = typename apply_impl<
    is_alias_metafunction<Function, sequence<Ts...>>::value,
    Function,
    sequence<Ts...>
>::type;

我们现在可以apply按如下方式使用元函数:

using type = apply<Function, Ts...>;

它将抽象出“遗留”元函数和现代(c++11)元函数之间的区别。

于 2013-07-18T13:36:54.383 回答