示例来自与cppreference.com,代码如下:
template<class T, T::type n = 0>
class X;
struct S
{
using type = int;
};
using T1 = X<S, int, int>; // error: too many arguments
using T2 = X<>; // error: no default argument for first template parameter
using T3 = X<1>; // error: value 1 does not match type-parameter
using T4 = X<int>; // error: substitution failure for second template parameter
using T5 = X<S>; // OK
第一行 T::type 直接报语法错误,网上也没找到与这个示例类似的说明
GCC 9 以下的编译器要加 typename
进行消歧
template<class T, typename T::type n = 0>
class X {
};
struct S
{
using type = int;
};
int main()
{
X<S, 1> x;
}
C++20 简化了 typename
,在这个语境下可以不用加 typename
。
用 GCC 9 以上的编译器可以直接编译通过。
确实是这样,谢谢!
把他改成这样就不报错了:
`template<class T, T n = 0>`