根据odr-use defintion ,如果引用绑定(bind)到对象,则该对象是 odr-used。这就是我相信 f 使 S::x odr-used 的原因。我不明白的是,这与 comma operator 有何不同?它还将其参数绑定(bind)到引用参数
struct S {
static const int x = 0; // static data member
// a definition outside of class is required if it is odr-used
};
const int& f(const int& r);
int n = b ? (1, S::x) // S::x is not odr-used here
: f(S::x); // S::x is odr-used here: a definition is required
最佳答案
这些只是关于如何在类中定义重载逗号运算符的示例。当您绑定(bind)到参数时,使用这种重载必然会触发 odr-use。
该用法尚未写入您的程序中,也根本没有运算符重载。
你只是在使用 the built-in comma operator .
(一个更有趣的问题可能是这个运算符最右边的操作数是否仍然使用 odr,因为从 the wording 看来它是这样的!请记住,不需要违反 odr 使用生成构建错误,在某些情况下它不会。)。
我认为 cppreference 页面在这方面可能有点不清楚。
关于c++ - 逗号运算符 odr- 使用它的参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52893454/