Is use in an unused default member initializer still an odr-use?

251 views Asked by At

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

1

There are 1 answers

0
Davis Herring On BEST ANSWER

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 declared B::B() is never defined, but the default member initializer is still an odr-use like it is here.)