NOTES: C + + namespaces

Source: Internet
Author: User

Content from C + + primer 5th

Large programs often use several independently developed libraries, which in turn define a large number of global names, inevitably with name collisions.

Namespaces: Provides a more manageable mechanism for preventing name collisions. Namespaces are separated by a global namespace, where each namespace is a scope. By defining the name of a library in a namespace, you can avoid collisions of global names.

Defined:

Namespace space_name{

declarations and definitions, as long as they can appear in the global scope, can be in the namespace

Mainly classes, variables, functions, templates, other namespaces

}

The namespace ends without semicolons.

The name of the namespace must also remain unique within the scope in which it is defined.

Namespaces cannot be defined inside classes and functions.

Each namespace is a scope.

The definition of a namespace can be divided into several parts without having to define it once.

Like what:

Namespace nsp{
}

It is possible to define a new namespace named NSP, or to add some new members to a namespace that already exists.

Template specializations must be defined in the namespace to which the original template belongs.

inline namespaces

C++11 New Standard

Unlike normal nested namespaces, names in an inline namespace can be used directly by the outer namespace, that is, without prefixing the namespace with the name of the inline namespace.

Inline namespace space_name{

}

Inline must appear in the namespace where it was first defined and can be written inline or not when the namespace is opened later.

Unnamed namespaces

Refers to the keyword namespace directly behind {}

Variables defined in an unnamed namespace have a static life cycle: Created before the first use, until the end of the program is destroyed.

NOTES: C + + namespaces

Related Article

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.