Assume I have the following code snippet:
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)
If we uncomment the line (1), we get a compile-time error "incomplete type T", and it seems to be clear: class Bar
instantiation is launched by (4), and at that point class Foo
is only forward-declared by (3) and not yet defined by (5).
But if the line (1) is commented out, then this code compiles with no errors, and it confuses me: (4) is an explicit template instantiation definition and it forces the compiler to generate void method()
code, and line (2) should also generate the same error, since the definition of Foo
is made later in (5).
What do I miss, why does the code from the snippet compiles?
UPDATE: Code compiles under GCC 8.2.0 and MSVC 19.16.27025.1, but under Clang 7.0.0 it gives an "incomplete type" error.