作者:十点半的 | 来源:互联网 | 2022-12-05 15:58
标题中的问题很清楚.更具体地说,请考虑以下示例:
#include
template
struct is_complete_helper {
template
static auto test(U*) -> std::integral_constant;
static auto test(...) -> std::false_type;
using type = decltype(test((T*)0));
};
template
struct is_complete : is_complete_helper::type {};
// The above is an implementation of is_complete from /sf/ask/17360801/
template class X;
static_assert(!is_complete>::type{});
// X should be implicitly instantiated here, an incomplete type
template class X {};
static_assert(!is_complete>::type{}); // #1
X ch; // #2
此代码与GCC和Clang编译.
根据[temp.inst]/1:
除非已明确实例化或明确专门化类模板特化,否则在需要完全定义的对象类型的上下文中引用特化时或者类类型的完整性影响程序的语义时,将隐式实例化类模板特化..
X
由于static_assert(!is_complete>::type{})
生成不完整类型而隐式实例化.
然后,在定义之后X
,#1
建议X
不再实例化(仍然不完整),同时#2
表明X
确实再次实例化(成为完整类型).
如果已经隐式实例化了特化,则会隐式实例化它吗?为什么#1
和之间存在差异#2
?
欢迎对该标准进行解释.