【发布时间】:2021-10-22 00:43:38
【问题描述】:
以下内容:
template< typename >
struct S;
template< typename T >
S< T >& f (S< T >& s) {
const typename S< T >::nested ignore;
return s;
}
template S< char >& f (S< char >&);
template< typename >
struct S {
struct nested { };
};
使用 gcc 编译,但不使用 clang:
$ clang -c /tmp/t.cpp
/tmp/t.cpp:6:20: error: implicit instantiation of undefined template 'S<char>'
const typename S< T >::nested ignore;
^
/tmp/t.cpp:10:21: note: in instantiation of function template specialization 'f<char>' requested here
template S< char >& f (S< char >&);
^
/tmp/t.cpp:2:8: note: template is declared here
struct S;
^
1 error generated.
我相信 clang 是对的,在实例化时,函数 f 引用了 S 的不完整定义。OTOH,稍后对 S 的专门化可能提供正确的定义,使依赖的“嵌套”格式良好.有意见吗?
【问题讨论】:
-
f<char>有两个实例化点:在显式实例化定义处和 TU 末尾。根据 [temp.point]/p8,您的代码是格式错误的 NDR;两个编译器都是正确的。