gpt4 book ai didi

c++ - 我只是无法理解 DR 712

转载 作者:可可西里 更新时间:2023-11-01 17:40:01 26 4
gpt4 key购买 nike

DR 712负责将 C++11 中 [basic.def.odr]/2 的措辞更改为今天的当前措辞,在 [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::b使用(odr-使用),因此,相应的静态成员struct S不需要 需要在 namespace 范围内定义。但这与 DR 所说的完全相反。我错过了什么???

最佳答案

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

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

因此,如果“立即应用”为假,则 UNLESS 为假,因此会被使用。

关于c++ - 我只是无法理解 DR 712,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42516175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com