Consider this example from cppreference:
struct S { static const int x = 1; }; void f() { &S::x; } // discarded-value expression does not odr-use S::x
I agree that &S::x
is a discarded-value expression, since the standard says (9.2, paragraph 1 [stmt.expr] from n4700)
Expression statements have the form
expression-statement: expression_opt ;
The expression is a discarded-value expression (Clause 8)...
However, is that enough for S::x
to not be odr-used? 6.2, paragraph 3 [basic.def.odr] states
A variable
x
whose name appears as a potentially-evaluated expressionex
is odr-used byex
unless
- ...
- if
x
is an object,ex
is an element of the set of potential results of an expressione
, where either
- the lvalue-to-rvalue conversion (7.1) is applied to
e
, ore
is a discarded-value expression (Clause 8).
The problem is that the discarded-value expression &S::x
has no potential results (which means that S::x
is not a potential result of &S::x
), as you can see from 6.2, paragraph 2 [basic.def.odr]:
... The set of potential results of an expression
e
is defined as follows:
- If
e
is an id-expression (8.1.4), the set contains onlye
.- If
e
is a subscripting operation (8.2.1) with an array operand, the set contains the potential results of that operand.- ...
- Otherwise, the set is empty.
Then, how can you explain that S::x
is not odr-used?
It is indeed odr-used. Your analysis is correct (and I fixed that example a while ago).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With