c++ - Use case for initialized extern variable -


i realized can define extern variable, like:

source.cpp

extern int = 42; // definition, can remove `extern` 

main.cpp

#include <iostream>  extern int i; // declaration  int  main() {     std::cout << << std::endl; } 

i can compile , link program,

g++ main.cpp source.cpp 

and runs , correctly displays 42. warning is

warning: 'i' initialized , declared 'extern' (gcc)

warning: 'extern' variable has initializer (clang)

using int = 42; in source.cpp has same overall effect.

my question: there non-trivial use case variables defined extern (not declared defined in translation unit)? such definition standard compliant?

the extern specifier useful in conjunction variables have internal linkage without it, such constants @ namespace scope:

a.cpp:

extern const int n = 10;  // external linkage 

b.cpp:

int main() {     extern const int n;   // finds n defined in other tu     return n; } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

excel - I can't get the attachement of the email PHP -

node.js - Express and Redis - If session exists for this user, don't allow access -