Is use in a default member initializer still an odr-use, even if the default member initializer is not used by any constructor?
For example, is this program ill-formed because g<A>
is odr-used and therefore its definition implicitly instantiated?
template<typename T>
void g() { sizeof(T); }
struct A;
struct B {
B() : i{} {};
int i = (&g<A>, 0);
};
int main() { }
MSVC thinks no. Clang, GCC and ICC think yes. https://godbolt.org/z/zrr9oEdfe
As stated in the comments,
g<A>
is odr-used. However, there is a definition for it available, so there is no non-diagnosable violation here; MSVC is wrong to accept it. (This is true even without the constructor declaration; the implicitly declaredB::B()
is never defined, but the default member initializer is still an odr-use like it is here.)