【发布时间】:2019-01-01 17:50:32
【问题描述】:
假设我有以下代码 sn-p:
template <class T>
class Bar
{
// static_assert(sizeof(T) > 0); // (1)
public:
void method()
{
static_assert(sizeof(T) > 0); // (2)
}
};
class Foo; // (3)
template class Bar<Foo>; // (4)
class Foo{}; // (5)
如果我们取消注释第 (1) 行,我们会得到一个编译时错误“不完整类型 T”,并且似乎很清楚:class Bar 实例化由 (4) 启动,此时 class Foo仅由 (3) 前向声明,尚未由 (5) 定义。
但是如果第(1)行被注释掉,那么这段代码编译没有错误,这让我很困惑:(4)是一个显式模板实例化定义,它强制编译器生成void method()代码,并且行(2) 也应该产生同样的错误,因为Foo 的定义是在 (5) 后面进行的。
我错过了什么,为什么 sn-p 中的代码可以编译?
更新:代码在 GCC 8.2.0 和 MSVC 19.16.27025.1 下编译,但在 Clang 7.0.0 下会出现“不完整类型”错误。
【问题讨论】:
-
用什么工具链编译(因为它用 vc++ (19.00.24215.1) 按原样呕吐?包括在你的问题中。
-
@WhozCraig 谢谢,我添加了描述。事实证明,clang 不会编译这个。
标签: c++ templates incomplete-type template-instantiation