c++ - Does "potentially-evaluated" means the same as "odr-used" in C++03? -
given example:
#include <iostream> class { public: static const int numberofwheels = 4; }; // const int a::numberofwheels; int main() { std::cout << a::numberofwheels << std::endl; }
is formally undefined behavior(ub) since a::numberofwheels
used without definition? (see here). c++03 states:
the member shall still defined in namespace scope if used in program , namespace scope definition shall not contain initializer.
i found definition of used in c++03 rather confusing, points potentially-evaluated expression:
an object or non-overloaded function used if name appears in potentially-evaluated expression.
from wild guess excludes expressions such as:
sizeof(a::numberofwheels) ; typeid(a::numberofwheels).name() ;
but not expression overloaded <<
operator above.
from these 2 defect reports seems intent should similar odr-used: defect report 48: definitions of unused static members says (emphasis mine going forward):
originally, static data members still had defined outside class whether used or not.
but restriction supposed lifted static data members need not defined outside class unless used in manner requires definition, in same manner namespace-scope variables. in particular, if integral/enum const static data member initialized within class, , address never taken, agreed no namespace-scope definition required.
this modified 3.2p2
follows:
an expression potentially evaluated unless it appears integral constant expression required (see 5.19 [expr.const] ), operand of sizeof operator (5.3.3 [expr.sizeof] ), or operand of typeid operator , expression not designate lvalue of polymorphic class type (5.2.8 [expr.typeid] ).
and defect report 82: definition of "using" constant expression :
the wording in 3.2 basic.def.odr paragraph 2 "potentially evaluated" incomplete. not distinguish between expressions used "integral constant expressions" , not; nor distinguish between uses in objects address taken , in not. (a suitable definition of "address taken" written without saying "address".)
but wording in standard not modified encompassed stated intent in either defect report , not clear why, added exception integral constant expression required.
update
defect report 454: when definition of static data member required? synced wording of standard intent expressed in defect report 48 , says:
as result of resolution of core issue 48, current c++ standard not in sync existing practice , user expectations far definitions of static data members having const integral or const enumeration type concerned. current implementations require definition if address of constant taken. example:
void f() { std::string s; ... // current implementations don't require definition if (s.find('a', 3) == std::string::npos) { ... }
to letter of standard, though, above requires definition of npos, since expression std::string::npos potentially evaluated. think problem solved simple changes 9.4.2 class.static.data paragraph 4, 9.4.2 class.static.data paragraph 5 , 3.2 basic.def.odr paragraph 3.
so c++03 section [basic.def.odr] meant cover close consider odr-used rules c++11. there subsequent changes c++11 rules via defect report 712 bring c++11 closer c++14 rules.
Comments
Post a Comment