模板别名可以用于部分专用化吗

can template alias be used for partial specialization?

本文关键字:专用 用于部 别名      更新时间:2023-10-16

给定一个模板别名

template<unsigned U>
using uint_ = integral_constant<unsigned,U>;

的部分专业化

template<class T,class P>
struct size{};

作为

template <class T,unsigned U>
struct size<T,uint_<U>>{};

为clang 3.1生成一个警告template parameter can not be deduced,而使用gcc 4.7 没有生成任何警告

那么,这是格式错误的代码吗?

代码在C++11中非常好。Clang的警告可以忽略。

另一个人说这是Clang bug。如果你像这个一样更改使用声明,你可以解决这个问题

template<unsigned T, unsigned U = T>
using uint_ = integral_constant<unsigned,U>;

作为一个有根据的猜测,显然Clang没有正确更新type-id中出现的模板参数的标识。因此,在您的示例中,它认为结果类型uint_<U>引用了部分专业化的第一个参数(因为在uint_中是这样,但在使用点上不是这样)。或者,您可以在使用时交换订单

template <unsigned U,class T>
struct size<T,uint_<U>>{};