12

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

user17732522
  • 53,019
  • 2
  • 56
  • 105

1 Answers1

8

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.)

Davis Herring
  • 36,443
  • 4
  • 48
  • 76