简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM