basic.def.odr 部分是否存在缺陷
Is it a defect in basic.def.odr section
struct S { static const int x = 0; };
int main(){
S obj;
int v = obj.x;
}
考虑上面的代码,obj.x
是 odr-use 吗?根据看段basic.def.odr#3
A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion to x yields a constant expression that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion is applied to e, or e is a discarded-value expression.
我们有这样的分析:
在我的代码中,变量名是x,也就是上面引号中的x,表达式obj.x
就是引号中的ex。那么,obj.x
是一个 潜在求值表达式 吗?是的,是的,因为这个:
An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.
obj.x
既不是未计算的操作数也不是其子表达式。所以 obj.x
是一个潜在评估的表达式。应用左值到右值 x
确实会产生一个常量表达式,因为它是由 0
初始化的。我怀疑的是以下内容,即
and, if x is an object, ex is an element of the set of potential results of an expression e
我们假设 e 是 obj.x
因为左值到右值的转换将应用于它。 e 的一组潜在结果是什么?如下:
basic.def.odr#2
The set of potential results of an expression e is defined as follows:
- If e is a class member access expression, the set contains the potential results of the object expression.
因为 e 是一个 class 成员访问表达式。因此它的潜在结果集是对象表达式。
Abbreviating postfix-expression.id-expression as E1.E2, E1 is called the object expression.
所以,这里的对象表达式是obj
,因此表达式obj.x
的潜在结果集包含obj
。显然,这里的ex是xand if e is
obj.xthen its set of potential results of expression would be
obj. So, what the expression
eof which the ex
x`是潜在结果集的一个元素?照着看basic.def.odr#2,没找到。
我非常确定左值到右值的转换应用于 obj.x
整个表达式。但是,所有 compiler 都同意使用 obj.x
是 none odr-use。是标准的缺陷吗?
你不能依赖编译器来告诉你它是否是 odr-use;如果是,并且您还没有定义变量,则程序格式错误,不需要诊断。也就是说,这似乎是 C++17 中的一个缺陷,因为使用 class 成员访问权限引用静态成员变量的可能性(与 qualified-id) 被忽略了。
struct S { static const int x = 0; };
int main(){
S obj;
int v = obj.x;
}
考虑上面的代码,obj.x
是 odr-use 吗?根据看段basic.def.odr#3
A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion to x yields a constant expression that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion is applied to e, or e is a discarded-value expression.
我们有这样的分析:
在我的代码中,变量名是x,也就是上面引号中的x,表达式obj.x
就是引号中的ex。那么,obj.x
是一个 潜在求值表达式 吗?是的,是的,因为这个:
An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.
obj.x
既不是未计算的操作数也不是其子表达式。所以 obj.x
是一个潜在评估的表达式。应用左值到右值 x
确实会产生一个常量表达式,因为它是由 0
初始化的。我怀疑的是以下内容,即
and, if x is an object, ex is an element of the set of potential results of an expression e
我们假设 e 是 obj.x
因为左值到右值的转换将应用于它。 e 的一组潜在结果是什么?如下:
basic.def.odr#2
The set of potential results of an expression e is defined as follows:
- If e is a class member access expression, the set contains the potential results of the object expression.
因为 e 是一个 class 成员访问表达式。因此它的潜在结果集是对象表达式。
Abbreviating postfix-expression.id-expression as E1.E2, E1 is called the object expression.
所以,这里的对象表达式是obj
,因此表达式obj.x
的潜在结果集包含obj
。显然,这里的ex是xand if e is
obj.xthen its set of potential results of expression would be
obj. So, what the expression
eof which the ex
x`是潜在结果集的一个元素?照着看basic.def.odr#2,没找到。
我非常确定左值到右值的转换应用于 obj.x
整个表达式。但是,所有 compiler 都同意使用 obj.x
是 none odr-use。是标准的缺陷吗?
你不能依赖编译器来告诉你它是否是 odr-use;如果是,并且您还没有定义变量,则程序格式错误,不需要诊断。也就是说,这似乎是 C++17 中的一个缺陷,因为使用 class 成员访问权限引用静态成员变量的可能性(与 qualified-id) 被忽略了。