c++ - What name lookup rule apply to the name in the definition of static const data member -


there quote described name lookup applied data member of const static data member after quialified-id of data member:

sec. 3.4.1/13:

a name used in definition of static data member of class x (9.4.2) (after qualified-id of static member) looked if name used in member function of x.

but can define in-class static const data member:

class {     static const int = x;//x name defined arbitrary const int }; 

rule sec. 3.4.1/13 not apply name x lookup. what rule applied actually? please give reference corresponding clause standard.

for instance, following code valid:

#include <stdio.h>  const int a=5; class { public:     static const int b=a;//b unqualified-id of data-member };  int main(){ printf("%d\n",a::b); } //5 

the following code valid:

#include <stdio.h>  class { public:     static const int a=7;     static const int b=a; };  int main(){ printf("%d\n",a::b); } //7 

but following invalid:

#include <stdio.h>  class { public:     static const int b=a; //error: not declare in scope.     static const int a=7; };  int main(){ printf("%d\n",a::b); } 

it unclear rules applied lookup of a.

an in-class declaration of static data member not definition if includes initializer. therefore §3.4.1/13 not apply example.

name lookup in class scope defined rules of scopes reach class scope. §3.3.7/1.1:

  1. the potential scope of name declared in class consists not of declarative region following name’s point of declaration, of function bodies, default arguments, exception-specifications, , brace-or-equal-initializers of non-static data members in class (including such things in nested classes).

  2. a name n used in class s shall refer same declaration in context , when re-evaluated in completed scope of s. no diagnostic required violation of rule.

  3. if reordering member declarations in class yields alternate valid program under (1) , (2), program ill-formed, no diagnostic required.

the purpose of these rules allow member function access member variables declared later in class. because rules don't mention initializers of static data members, name lookup static data member defaults 3.4.1/7:

a name used in definition of class x outside of member function body, default argument, exception- specification, brace-or-equal-initializer of non-static data member, or nested class definition shall declared in 1 of following ways:

the list boils down "a name must member of same class, or declared in enclosing scope." in first example, a declared in enclosing namespace, found. in second example, a declared before used.

but in third example, used before declared illegal — , if there declaration of a in enclosing namespace, undefined behavior due 3.3.7/1.2.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -