in n4296, 3.2 [basic.def.odr]p3:
a variable
xname appears potentially-evaluated expressionexodr-usedexunless applying lvalue-to-rvalue conversionxyields constant expression not invoke non-trivial functions and, ifxobject,exelement of set of potential results of expressione, either lvalue-to-rvalue conversion appliede, orediscarded-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:
- either
exnot potentially evaluated, or- all of following must fulfilled:
- "applying lvalue-to-rvalue conversion
xyields constant expression not invoke non-trivial functions" and- "
exelement of set of potential results of expressione" and either of following holds:
- "either lvalue-to-rvalue conversion applied
e"- "or
ediscarded-value expression"
and 2 cppreference http://en.cppreference.com/w/cpp/language/definition
a variable
xin potentially-evaluated expressionexodr-used unless any of following true:
applying lvalue-to-rvalue conversion
xyields constant expression doesn't invoke non-trivial functions
xobject , ex 1 of potential results of larger expressione, 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
Post a Comment