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 -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -