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

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

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/

相关文章:

c++ - std::poisson_distribution 中可能存在的构造错误

c++ - 在数字的右边使用左移运算符

c++ - 什么是严格的别名规则?

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

c++ - reinterpret_cast 从原始内存中获取的指针重新分配是否会导致 UB?

c++ - 在放置新数据之前初始化数据是未定义的行为吗?

c++ - 测试构造函数初始化列表

c++ - 我想从 C++ 调用 *delegate.m 中的方法

c++ - 当函数接受 T&& 的参数并传递左值时,模板参数 T 是否应该解析为 T&?

c++ - Visual Studio 2013 Update 2 中的 C++ 改进是什么?