我只是无法理解 DR 712

I just can not understand DR 712

DR 712 was responsible for the change in the wording of [basic.def.odr]/2 in C++11 to the current wording today, in [basic.def.odr]2 and 3。但是我还在尝试理解更改的原因,如DR中所述,如下:

712. Are integer constant operands of a conditional-expression “used?”

In describing static data members initialized inside the class definition, 9.2.3.2 [class.static.data] paragraph 3 says,

The member shall still be defined in a namespace scope if it is used in the program...

The definition of “used” is in 3.2 [basic.def.odr] paragraph 1:

    An object or non-overloaded function whose name appears as a potentially-evaluated
    expression is used unless it is an object that satisfies the requirements for appearing in a     constant expression (5.20 [expr.const]) and the lvalue-to-rvalue conversion (4.1 [conv.lval])
    is immediately applied.

Now consider the following example:

 struct S {
      static const int a = 1;
      static const int b = 2;
 };
 int f(bool x) {
      return x ? S::a : S::b;
 }

According to the current wording of the Standard, this example requires that S::a and S::b be defined in a namespace scope. The reason for this is that, according to 5.16 [expr.cond] paragraph 4, the result of this conditional-expression is an lvalue and the lvalue-to-rvalue conversion is applied to that, not directly to the object, so this fails the “immediately applied” requirement. This is surprising and unfortunate, since only the values and not the addresses of the static data members are used. (This problem also applies to the proposed resolution of issue 696.)

嗯,如果“立即应用”要求失败,那么条件表达式中的表达式S::aS::bnot used(odr-used ),因此,struct S 的各个静态成员将 而不是 需要在命名空间范围内定义。但这与 DR 所说的恰恰相反。什么是我不见了???

我认为您错过了 used 定义的要点:

it is used UNLESS ( (const) AND (immediately applied) )

因此,如果 "immediately applied" 为假,则 UNLESS 为假,因此使用它。

An object or non-overloaded function whose name appears as a potentially-evaluated expression is used unless[...]

好吧,S::aS::b 出现在 f 中的潜在求值表达式中。

所以它们被使用,除非以下任一例外情况适用。

[...] it is an object that satisfies the requirements for appearing in a constant expression (5.20 [expr.const]) and the lvalue-to-rvalue conversion (4.1 [conv.lval]) is immediately applied.

嗯,这是一个满足出现在常量表达式中的对象。所以它通过了那个测试。

但是不会立即应用左值到右值的转换。

所以它没有通过测试。

由于整个部分都在 "unless" 下,失败意味着前一个案例适用;该对象被认为是 "used".