简而言之:此代码可以在 clang++ (10.0) 中编译,但不能在 g++ (12.2) 中编译。哪一个是正确的?
template<typename... T>
struct V{
template<template<typename...> class U=V, typename T2>
static void foo(T2){}
};
int main(){
V<int>::foo(0);
}
仅供参考,错误是(使用 gcc 12.2):
<source>:3:42: error: invalid use of type 'V<T>' as a default value for a template template-parameter
3 | template<template<typename...> class U=V, typename T2>
| ^
这里的相关主题是注入类名。来自cppreference:
在类模板中,注入的类名称可以用作引用当前模板的模板名称,也可以用作引用当前实例化的类名称。
这一点以及后来的 gcc 版本接受代码的事实暗示了 gcc 中的编译器错误,该错误已在较新版本中修复。