c++ - 逗号运算符 odr- 使用它的参数吗?

标签 c++ language-lawyer one-definition-rule

根据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/

相关文章:

c++ - LINUX:可以通过 LAN 进行管道传输吗?如果是这样,是否可取?还有哪些其他选择?

c++ - 将转发 lambda 转换为函数指针

c++ - glBindBuffer 崩溃 - 使用 glew 的 VBO 实现

c++ - 如何为具有引用成员变量的类实现 operator=()?

c++ - DirectxTK SpriteBatch 和视口(viewport)居中

c++ - [expr.cond]/2 是不是少了什么?

c++ - 为什么当 typedef const 指针与 extra const 一起使用时编译器不报错?

c++ - 内联模板特化

c++ - 具有预处理器分支实现的结构是否违反了 ODR?

c++ - 这可以在 C++ 中合法地完成吗?