Tutorial by Examples

A C++ namespace is a collection of C++ entities (functions, classes, variables), whose names are prefixed by the name of the namespace. When writing code within a namespace, named entities belonging to that namespace need not be prefixed with the namespace name, but entities outside of it must use t...
Creating a namespace is really easy: //Creates namespace foo namespace Foo { //Declares function bar in namespace foo void bar() {} } To call bar, you have to specify the namespace first, followed by the scope resolution operator ::: Foo::bar(); It is allowed to create one names...
A useful feature of namespaces is that you can expand them (add members to it). namespace Foo { void bar() {} } //some other stuff namespace Foo { void bar2() {} }
The keyword 'using' has three flavors. Combined with keyword 'namespace' you write a 'using directive': If you don't want to write Foo:: in front of every stuff in the namespace Foo, you can use using namespace Foo; to import every single thing out of Foo. namespace Foo { void bar() {} ...
When calling a function without an explicit namespace qualifier, the compiler can choose to call a function within a namespace if one of the parameter types to that function is also in that namespace. This is called "Argument Dependent Lookup", or ADL: namespace Test { int call(int i)...
C++11 inline namespace includes the content of the inlined namespace in the enclosing namespace, so namespace Outer { inline namespace Inner { void foo(); } } is mostly equivalent to namespace Outer { namespace Inner { void foo(); } u...
An unnamed namespace can be used to ensure names have internal linkage (can only be referred to by the current translation unit). Such a namespace is defined in the same way as any other namespace, but without the name: namespace { int foo = 42; } foo is only visible in the translation uni...
C++17 namespace a { namespace b { template<class T> struct qualifies : std::false_type {}; } } namespace other { struct bob {}; } namespace a::b { template<> struct qualifies<::other::bob> : std::true_type {}; } You can enter both the a and b n...
This is usually used for renaming or shortening long namespace references such referring to components of a library. namespace boost { namespace multiprecision { class Number ... } } namespace Name1 = boost::multiprecision; // Both Type declarations are equivale...
Alias Declaration are affected by preceding using statements namespace boost { namespace multiprecision { class Number ... } } using namespace boost; // Both Namespace are equivalent namespace Name1 = boost::multiprecision; namespace Name2 = multiprecision; ...
A namespace can be given an alias (i.e., another name for the same namespace) using the namespace identifier = syntax. Members of the aliased namespace can be accessed by qualifying them with the name of the alias. In the following example, the nested namespace AReallyLongName::AnotherReallyLongNam...

Page 1 of 1