Consider the following code:
#include <iostream>
struct M {
M() { std::cout << "M\n"; }
};
template <class T>
struct Test {
Test() { std::cout << "Test\n"; }
inline static auto m = M{};
};
int main() {
Test<int> t1;
//Test t;
//(void)&t1.m;
}
Using the latest GCC or Clang the only "Test" is printed out. But if we use an address of m
object (uncomment the last line (void)&t1.m;
) or transform Test
class template into the regular (non-templated) class then the M
constructor was called.
Is this behaviour allowed by the C++ standard? Any quotes?
Yes, it's spelled out in the standard.
Since your example doesn't use the static data member at all, its definition is never fully instantiated.