标题中的问题已经很清楚了。更具体地说,请考虑以下示例:
#include <type_traits>
template <typename T>
struct is_complete_helper {
template <typename U>
static auto test(U*) -> std::integral_constant<bool, sizeof(U) == sizeof(U)>;
static auto test(...) -> std::false_type;
using type = decltype(test((T*)0));
};
template <typename T>
struct is_complete : is_complete_helper<T>::type {};
// The above is an implementation of is_complete from https://stackoverflow.com/a/21121104/5376789
template<class T> class X;
static_assert(!is_complete<X<char>>::type{});
// X<char> should be implicitly instantiated here, an incomplete type
template<class T> class X {};
static_assert(!is_complete<X<char>>::type{}); // #1
X<char> ch; // #2
除非类模板特化已被显式实例化或显式特化,否则当在需要完全定义的对象类型的上下文中引用该特化或当类类型的完整性影响程序的语义时,类模板特化将被隐式实例化.
X<char>
由于 被隐式实例化static_assert(!is_complete<X<char>>::type{})
,这会生成不完整的类型。
然后,在 , 的定义之后X
,#1
表明X<char>
没有再次实例化(仍然不完整),而#2
表明X<char>
确实再次实例化(成为完整类型)。
如果特化已经被隐式实例化,它是否会被隐式实例化?#1
为什么和之间有区别#2
?
欢迎对标准进行解释。