简体   繁体   English

在未使用的默认成员初始值设定项中使用仍然是 ODR 使用吗?

[英]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?在默认成员初始值设定项中使用是否仍然是 odr-use,即使默认成员初始值设定项未被任何构造函数使用?

For example, is this program ill-formed because g<A> is odr-used and therefore its definition implicitly instantiated?例如,这个程序是否格式错误,因为g<A>是 odr-used 并且因此它的定义被隐式实例化?

template<typename T>
void g() { sizeof(T); }

struct A;

struct B {
    B() : i{} {};

    int i = (&g<A>, 0);
};

int main() { }

MSVC thinks no. MSVC 认为不。 Clang, GCC and ICC think yes. Clang、GCC 和 ICC 认为是的。 https://godbolt.org/z/zrr9oEdfe https://godbolt.org/z/zrr9oEdfe

As stated in the comments, g<A> is odr-used. 正如评论中所述, g<A>是 odr-used。 However, there is a definition for it available, so there is no non-diagnosable violation here;但是,有可用的定义,因此这里没有不可诊断的违规行为; MSVC is wrong to accept it. MSVC接受是错误的。 (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.) (即使没有构造函数声明也是如此;隐式声明的B::B()从未定义,但默认成员初始化器仍然是一个 odr-use,就像这里一样。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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