C ++ anonymous namespace

Source: Internet
Author: User

When defining a namespace, you can ignore the namespace Name:

Namespce {char C; int I; double D;} the compiler internally generates a unique name for this namespace and generates a using command for this anonymous namespace. The above code is equivalent to: namespace _ unique_name _ {char C; int I; double D;} using namespace _ unique_name _; the name declared in the anonymous namespace will also be converted by the compiler and bound to the unique internal name (here _ unique_name _) generated by the compiler for this anonymous namespace. Another important thing is that these names have internal link attributes, which are the same as the Link Attributes of global names whose names are static, that is, the name scope is restricted in the current file, cannot be linked by using the extern declaration in another file. If you do not advocate using Global static to declare that a name has an internal link attribute, the anonymous namespace can be used as a better way to achieve the same effect. Note: The namespace has the external connection attribute, but the _ unique_name _ generated by the anonymous namespace cannot be obtained in other files. The unique name is invisible. in the new C ++ standard, anonymous namespaces are recommended instead of static, because static is used in different places and has different meanings, which may cause confusion. in addition, static cannot modify the class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.