c++ - One definition rule about class member access expression -


in n4296, 3.2 [basic.def.odr]p3:

a variable x name appears potentially-evaluated expression ex odr-used ex unless applying lvalue-to-rvalue conversion x yields constant expression not invoke non-trivial functions and, if x object, ex element of set of potential results of expression e, either lvalue-to-rvalue conversion applied e, or e discarded-value expression.

how explain paragraph? found 2 explanation.

1 here "trying understand [basic.def.odr]/2 in c++14 (n4140)"

let's split steps: occurrence of variable `x` in expression `ex` constitutes odr-use unless:
  1. either ex not potentially evaluated, or
  2. all of following must fulfilled:
    1. "applying lvalue-to-rvalue conversion x yields constant expression not invoke non-trivial functions" and
    2. "ex element of set of potential results of expression e" and either of following holds:
      1. "either lvalue-to-rvalue conversion applied e"
      2. "or e discarded-value expression"

and 2 cppreference http://en.cppreference.com/w/cpp/language/definition

a variable x in potentially-evaluated expression ex odr-used unless any of following true:

  • applying lvalue-to-rvalue conversion x yields constant expression doesn't invoke non-trivial functions

  • x object , ex 1 of potential results of larger expression e, larger expression either discarded-value expression or lvalue-to-rvalue conversion

first answer 2 rules and, other any. 1 right?

please split rules steps explain code:

struct s { static const int x = 0; }; extern s s;// no definition of s int = s.x;// s odr-used? x odr-used?             // gcc 5.1.0 ok 

cppreference is wrong; clear language in standard (whichever version) both subclauses must hold. i've corrected it.

in example, s not constant expression (c++14: not satisfy requirements appearing in constant expression) odr-used. second subclause not arise.

meanwhile, x odr-used, because although possible use x in constant expression in appropriate context (e.g. array bound within definition of s); x not 1 of potential results of enclosing expression s.x, enclosing expression subject either discarded-value transformation or lvalue-to-rvalue conversion.

gcc might ok without definition of s or x, there's no requirement implementation diagnose every odr violation.


Comments