The question in the title is clear enough. To be more specific, consider the following example:
#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://mcmap.net/q/25289/-using-sfinae-to-check-if-the-type-is-complete-or-not-duplicate
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
This code compiles with GCC and Clang.
According to [temp.inst]/1:
Unless a class template specialization has been explicitly instantiated or explicitly specialized, the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program.
X<char>
is implicitly instantiated due to static_assert(!is_complete<X<char>>::type{})
, which generates an incomplete type.
Then, after the definition of X
, #1
suggests that X<char>
is not instantiated again (still incomplete) while #2
suggests that X<char>
is indeed instantiated again (becomes a complete type).
Is a specialization implicitly instantiated if it has already been implicitly instantiated? Why is there a difference between #1
and #2
?
An interpretation from the standard is welcome.
is_complete<X<char>>::type
depends of instantiation point (including EOF one). – Cassandra