【问题标题】:Does the comma operator odr-use its arguments?逗号运算符是否使用其参数?
【发布时间】:2018-10-19 13:34:22
【问题描述】:

根据odr-use defintion,如果引用绑定到对象,则该对象是odr-used。这就是我相信 f 使 S::x odr-used 的原因。我无法理解的是,这与comma operator 有何不同,后者还将其参数绑定到引用参数

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

【问题讨论】:

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


    【解决方案1】:

    这些只是示例,说明如何在类中定义重载的逗号运算符。当您绑定到参数时,这种重载的使用必然会触发 odr-use。

    这种用法并没有写入你的程序,也没有运算符重载。

    你只是在使用the built-in comma operator

    (一个更有趣的问题可能是该运算符的最右边的操作数是否仍然是 odr-used,因为从 the wording 看来,它在我看来就是这样!请记住,不需要违反 odr-use生成构建错误,在某些情况下不会。)。

    我认为 cppreference 页面在这方面可能有点不清楚。

    【讨论】:

    • [basic.def.odr]/4 很难解析,但好吧,也许这是对这种情况的豁免
    【解决方案2】:

    内置逗号不会将其操作数绑定到任何东西。

    重载的逗号可以,但重载的运算符只是拼写有趣的函数。

    【讨论】:

    • 关于标记为 language-lawyer 的问题,我希望得到权威来源的报价。
    • 准确地引用哪个声明?
    猜你喜欢
    • 2018-03-21
    • 2014-04-09
    • 2020-04-22
    • 2011-05-09
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多