Unnamed namespace in C++ primer (P. 791)

 

Concepts


  • Variables defined in an unnanmed namespace have static lifetime.
  • Unlike other namespaces, an unnamed namespace is local to a particular file and never spans multiple files.
  • Names defined in an unnamed namespace are used directly.
  • If an unnamed namespace is defined at the outermost scope in the file, then names in the unnamed namespace must differ from names defined at global scope.
  int i;  // global declaration for i
  namespace{
      int i;
  }
  // ambiguous:defined globally and in an unnested, unnamed namespace.
  i = 10;
  • The use of file static declarations is deprecated by the C++ standard. File statics should be avoided and unnamed namespaces used instead.

 

Static and Unnamed namespace

The keyword static applies only to the variables declarations and functions, not to the user-defined types.

posted @ 2022-03-29 17:06  Dy2MoRaw  阅读(34)  评论(0)    收藏  举报