Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is S::x not odr-used?

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 expression ex is odr-used by ex unless

  • ...
  • 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 (7.1) is applied to e, or
    • e 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 only e.
  • 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?

like image 269
b1sub Avatar asked Dec 23 '17 11:12

b1sub


Video Answer


1 Answers

It is indeed odr-used. Your analysis is correct (and I fixed that example a while ago).

like image 193
T.C. Avatar answered Oct 09 '22 23:10

T.C.