July 25th Wednesday (七月 二十五日 水曜日)

Traditional C++ Namespaces

  The first term is declarative region. A declarative region is a region in which declarations can be made.
For example, you can declare a global variable outside of any function. The declarative region for that
variable is the file in which it is declared.  If you declare a variable inside a function, its declarative
region is the innermost block in which it is declared.

  The second term is potential scope. The potential scope for a variable begins at its point of declaration
and extends to the end of its declarative region.  So the potential scope is more limited than the declarative
region because you can't use a variable above the point it is first defined.

  A variable, however, might not be visible everywhere in its potential scope. For example, it can be hidden
by another variable of the same name declared in a nested declarative region. For example, a local variable
declared in a function (the declarative region is the function) hides a global variable declared in the same
file (the declarative region is the file).  The portion of the program that actually can see the variable is
termed the scope, which is the way we've been using the term all along.

  C++'s rules about global and local variables define a kind of namespace hierarchy.  Each declarative region
can declare names that are independent of names declared in other declarative regions.  A local variable declared
in one function doesn't conflict with a local variable declared in a second function.


New Namespace Features

  What C++ now adds is the ability to create named namespaces by defining a new kind of declarative region,
one whose main purpose is to provide an area in which to declare names.  The names in one namespace don't
conflict with the same names declared in other namespaces, and there are mechanisms for letting other parts
of a program use items declared in a namespace.

  Namespaces can be located at the global level or inside other namespaces, but they cannot be placed in
a block.  Thus, a name declared in a namespace has external linkage by default (unless it refers to a constant).

  Namespaces are open, meaning you can add names to existing namespaces.


Using-Directive Versus Using-Declaration

  Using a using-directive to import all the names wholesale is not the same as using multiple using-declarations.
It's more like the mass application of a scope resolution operator. When you use a using-declaration, it is as if
the name is declared at the location of the using- declaration. If a particular name already is declared in a function,
you can't import the same name with a using-declaration.  When you use a using-directive, however, name resolution
takes place as if you declared the names in the smallest declarative region containing both the using-declaration
and the namespace itself.

  Simply, "using namespace ..." is alike to put or duplicate the namespace in the smallest region.  The namespace
is a entity, it includes various of declarations.

  Generally speaking, the using-declaration is safer to use because it shows exactly what names you are making available.
And if the name conflicts with a local name, the compiler lets you know.  The using-directive adds all names, even ones
you might not need.  If a local name conflicts, it overrides the namespace version, and you won't be warned.  Also,
the open nature of namespaces means that the complete list of names in a namespace might be spread over several locations,
making it difficult to know exactly which names you are adding.

  Namespace can also nest declare, "using ..." or "using namespace ..." in the body of a namespace.


  You can create an "unnamed namespace" by omitting the namespace name.

  This behaves as if it were followed by a using-directive; that is, the names declared in this namespace are in potential
scope until the end of the declarative region containing the unnamed namespace. In this respect, they are like global variables.
However, because the namespace has no name, you can't explicitly use a using-directive or using-declaration to make the names
available elsewhere.  In particular, you can't use names from an unnamed namespace in a file other than the one containing
the namespace declaration.  This provides an alternative to using static variables with internal linkage.  Indeed, the C++
standard deprecates the use of the keyword static in namespaces and global scope.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值