Skip to Main Content
C++ In a Nutshell
book

C++ In a Nutshell

by Ray Lischner
May 2003
Intermediate to advanced content levelIntermediate to advanced
808 pages
32h 24m
English
O'Reilly Media, Inc.
Content preview from C++ In a Nutshell

Name

namespace keyword — Declares namespace

Synopsis

               declaration := namespace-defn
               block-decl := namespace-alias-defn | using-directive
               namespace-defn ::= named-namespace-defn | unnamed-namespace-defn
               named-namespace-defn ::= namespace identifier { namespace-body }
unnamed-namespace-defn ::= namespace { namespace-body }
namespace-body ::= [declaration-seq]
namespace-alias-defn ::= namespace identifier = namespace-specifier ;
namespace-specifier ::= [::] [nested-name ::] namespace-name
               namespace-name ::= identifier
               using-directive ::= using namespace namespace-specifier ;

The namespace keyword can be used in a namespace definition, a namespace alias definition, or a using directive. A namespace is a scope for declarations of classes, templates, functions, objects, and other namespaces. Outside a namespace, you can refer to a name that is declared in the namespace by qualifying the name with the scope operator (::), such as ns::name, or with a using directive or declaration.

Multiple namespace declarations can name the same namespace, each one adding more declarations to the namespace. The standard namespace, std, is built this way, with many different headers all placing their declarations in the std namespace. A namespace can be anonymous, which prevents the enclosed declarations from being visible in other source files.

A namespace alias defines an identifier as a synonym for an existing namespace. See using for information on the using directive.

Example

namespace math_version_2 { const ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ High Performance

C++ High Performance

Viktor Sehr, Björn Andrist
Optimized C++

Optimized C++

Kurt Guntheroth
Mastering C++ Programming

Mastering C++ Programming

Jeganathan Swaminathan

Publisher Resources

ISBN: 059600298XSupplemental ContentErrata Page