c++ - Extern in class function -


my simple code looks like:

a.cpp:

#include <iostream>  namespace asd {     class b     {     public:         void ss()         {             extern int i;             std::cout << i;         }     }; }  int main() {     asd::b e;     e.ss(); } 

b.cpp:

int = 4; 

is code standard or no ? visual studio compiles without errors intel c++ compiler says: unresolved external symbol "int asd::i" (?i@asd@@3ha)

for more fun if change b.cpp to:

namespace asd {     int = 4; } 

then visual studio c++ 2013 says: unresolved external symbol "int i" (?i@@3ha)

but intel c++ compiler says ok :) proper version of code if want have extern in class member function (is legal ?) ?

edit: best results are, when change b.cpp to:

namespace asd {     int = 4; } int = 5; 

visual c++ prints 5, intel compiler 4 :)

it legal declare extern or static variable inside function. fix of b.cpp put namespace around definition of extern right fix, too.

visual studio c++ 2013 complains name outside asd namespace (check demangler see these characters around name i represent). incorrect, because declaration places i namespace asd.

c++ standard illustrates in section 3.5.7. using extern function example, illustrates rule of placement of name in enclosing namespace.

namespace x {     void p() {         q(); // error: q not yet declared         extern void q(); // q member of namespace x     }     void middle() {         q(); // error: q not yet declared     }     void q() { /* ... */ } // definition of x::q } void q() { /* ... */ } // other, unrelated q 

the comments on lines 4, 9, , 11 show name declared extern inside member function needs placed in enclosing namespace. good, self-contained test case illustrating bug in microsoft's compiler.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -