c++ - Deducing type of a global variable -


let's have global variable declared in header:

extern long global_var; 

it reasonable let compiler deduce type when variable defined in source:

auto global_var; 

i see @ least pros:

  • avoiding code duplication, ie when change type in header not have change in source file
  • less error prone, if variable not declared previously, not compile

similar work static members of class.

but not seem possible. there cons outweigh benefits?

one way avoid having remember type name of global_var , still right type compiler use decltype.

extern int global_var;   ...   decltype(global_var) global_var; 

the downside of approach you'll have make declaration of variable available compiler before definition.

of course, works static member variables of classes too.

see working @ http://ideone.com/w1wtov.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -