匹配可变参数非类型模板

时间:2017-05-30 13:21:52

标签: c++ templates c++14 variadic-templates non-type

我们说我有两个结构,FooBar

template<int...>
struct Foo{};

template<unsigned long...>
struct Bar{};

我想创建一个类型特征(称之为match_class),如果我传递两个Foo<...>类型或两个Bar<...>类型,则返回true,但如果我尝试混合它们则返回false:

int main()
{
    using f1 = Foo<1, 2, 3>;
    using f2 = Foo<1>;
    using b1 = Bar<1, 2, 3>;
    using b2 = Bar<1>;
    static_assert(match_class<f1, f2>::value, "Fail");
    static_assert(match_class<b1, b2>::value, "Fail");
    static_assert(!match_class<f1, b1>::value, "Fail");
}

对于C ++ 1z(clang 5.0.0和gcc 8.0.0),它足以做到这一点(Demo):

template<class A, class B>
struct match_class : std::false_type{};

template<class T, template<T...> class S, T... U, T... V>
struct match_class<S<U...>, S<V...>> : std::true_type{};

但在C ++ 14中我收到以下错误(相同的编译器 * Demo):

error: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct match_class<S<U...>, S<V...>> : std::true_type{};
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: non-deducible template parameter 'T'
template<class T, template<T...> class S, T... U, T... V>

问题:C ++ 14中的解决方法是什么?

理想情况下,测试类型特征的语法应保持不变。

次要问题: C ++ 14的行为是否正确? (或者我看到的C ++ 17未指定的行为?)

*请注意,MSVC 19.00.23506具有相同类型的失败Demo

3 个答案:

答案 0 :(得分:17)

在C ++ 14中,您无法推断出T

template<class T, template<T...> class S, T... U, T... V>
struct match_class<S<U...>, S<V...>> : std::true_type{};

但是在C ++ 17中,你可以。你看到的行为是正确的。

在C ++ 14中,由于无法推断T,因此需要一种明确提供它的方法。因此,您可能需要类模板本身来指示其非类型模板参数类型:

template <int...> struct Foo { using type = int; };
template <unsigned long...> struct Bar { using type = unsigned long; };

或者有一个外部特征。然后明确写出所有内容 - 两个类模板匹配,如果它们具有相同的非类型模板参数,那么也按顺序具有相同的类模板:

template <class... Ts> struct make_void { using type = void; };
template <class... Ts> using void_t = typename make_void<Ts...>::type;

template <class T1, class T2, class A, class B>
struct match_class_impl : std::false_type { };

template <class T, template <T...> class S, T... U, T... V>
struct match_class_impl<T, T, S<U...>, S<V...>> : std::true_type{};

template <class A, class B, class=void>
struct match_class : std::false_type { };

template <class A, class B>
struct match_class<A, B, void_t<typename A::type, typename B::type>>
    : match_class_impl<typename A::type, typename B::type, A, B>
{ };

这是添加对template auto的支持的结果。在C ++ 14中,[temp.deduct.type]包含:

  

无法从非类型模板参数的类型推断出模板类型参数。 [实施例:

template<class T, T i> void f(double a[10][i]);
int v[10][20];
f(v); // error: argument for template-parameter T cannot be deduced
     

-end example]

但在C ++ 17中,now reads

  

当从表达式推导出与从属类型声明的非类型模板参数P对应的参数的值时,P类型的模板参数是从值的类型。 [实施例:

template<long n> struct A { };

template<typename T> struct C;
template<typename T, T n> struct C<A<n>> {
  using Q = T;
};

using R = long;
using R = C<A<2>>::Q;           // OK; T was deduced to long from the
                                // template argument value in the type A<2>
     

- 结束示例] N类型中的T[N]类型为std​::​size_­t [实施例:

template<typename T> struct S;
template<typename T, T n> struct S<int[n]> {
  using Q = T;
};

using V = decltype(sizeof 0);
using V = S<int[42]>::Q;        // OK; T was deduced to std​::​size_­t from the type int[42]
     

- 结束示例]

答案 1 :(得分:6)

  

问题:C ++ 14中的解决方法是什么?

C ++ 14中可能的解决方法基于特征 作为一个最小的,工作的例子(甚至可能是愚蠢的,但它有助于得到这个想法):

#include <type_traits>
#include <utility>

template<int...>
struct Foo{};

template<unsigned long...>
struct Bar{};

template<typename>
struct traits;

template<int... V>
struct traits<Foo<V...>> { using type = Foo<0>; };

template<unsigned long... V>
struct traits<Bar<V...>> { using type = Bar<0>; };

template<typename T, typename U>
constexpr bool match = std::is_same<typename traits<T>::type, typename traits<U>::type>::value;

int main() {
    using f1 = Foo<1, 2, 3>;
    using f2 = Foo<1>;
    using b1 = Bar<1, 2, 3>;
    using b2 = Bar<1>;

    static_assert(match<f1, f2>, "Fail");
    static_assert(match<b1, b2>, "Fail");
    static_assert(!match<f1, b1>, "Fail");
}

作为旁注,在C ++ 17中,您可以简化以下内容:

template<template<auto ...> class S, auto... U, auto... V>
struct match_class<S<U...>, S<V...>> : std::true_type{};

关于错误背后的原因,@Barry's answer包含您需要理解的所有内容(像往常一样)。

答案 2 :(得分:1)

这是一个通用的C ++ 14解决方案,它不依赖于手动专用类型特征或扩展FooBar

模板元函数,它获取表示其参数类型的类模板的类型:

namespace detail
{
    // Type representing a class template taking any number of non-type template arguments.
    template <typename T, template <T...> class U>
    struct nontype_template {};
}

// If T is an instantiation of a class template U taking non-type template arguments,
// this has a nested typedef "type" that is a detail::nontype_template representing U.
template <typename T>
struct nontype_template_of {};

// Partial specializations for all of the builtin integral types.
template <template <bool...> class T, bool... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<bool, T>; };
template <template <char...> class T, char... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<char, T>; };
template <template <signed char...> class T, signed char... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<signed char, T>; };
template <template <unsigned char...> class T, unsigned char... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned char, T>; };
template <template <short...> class T, short... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<short, T>; };
template <template <unsigned short...> class T, unsigned short... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned short, T>; };
template <template <int...> class T, int... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<int, T>; };
template <template <unsigned int...> class T, unsigned int... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned int, T>; };
template <template <long...> class T, long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<long, T>; };
template <template <unsigned long...> class T, unsigned long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned long, T>; };
template <template <long long...> class T, long long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<long long, T>; };
template <template <unsigned long long...> class T, unsigned long long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned long long, T>; };

易于使用的别名模板:

// Alias template for nontype_template_of.
template <typename T>
using nontype_template_of_t = typename nontype_template_of<T>::type;

然后你可以像这样实现你的match_class特征:

template <class A, class B>
struct match_class : std::is_same<nontype_template_of_t<A>, nontype_template_of_t<B>> {};

DEMO