假设我有两个结构,Foo
和Bar
:
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 ++ 17的行为?)
*请注意,MSVC 19.00.23506具有相同的故障演示
在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
-端示例]
但是在C ++ 17中,它现在显示为:
当
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]
—结束示例]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句