C++C++11C++14C++17C++20Learn C++Syntax

What Is The Inline Namespace Feature In Modern C++?

What Is The Inline Namespace Feature In Modern C++

In modern C++, namespaces are a kind of library or framework. They are useful to hold the same name of classes, methods, and other entities in different namespaces. C++11 standard and other standards above, allow the inline keyword in a namespace definition and in this post, we explain how to use inline namespaces in modern C++.

What is a namespace in modern C++?

Namespaces in C++ allow users to group entities like classes, methods, variables and functions in a narrower namespace scope instead of using in the global scope. This prevents name conflicts between classes, methods, functions and variables in large projects. Namespaces organize the elements of developers into different logical scopes referred to by names. Thus, different users (or namespaces) may use same method names and variables in different running processes.

If you are new to namespaces, you can check this full post about namespace.

What is an inline namespace in modern C++?

C++11 standard and other standards above, allow the inline keyword in a namespace-definition. This is done in order to specify that members of the namespace can be defined and specialized as though they actually belong to the enclosing namespace.

In C++11 and the standards above, this new construction is called inline namespace and solves the problem where templates could only be specialized in their actual namespace, and not in the namespace they have been imported.

Here is the syntax for the inline namespace (since C++11)

Since C++17, you can use optional sequence of any number of attributes as below,

If you want to extend the definition of an existing namespace (adding new classes, methods or templates, etc.), the lookup for the identifier is used in the namespace definition, it is used to resolve to a namespace name where the namespace is declared as a member of the enclosing namespace or of an inline namespace within an enclosing namespace.

Note that, any namespace member (class, method, or property) that is declared within a namespace definition can be defined or redeclared outside of it by using explicit qualification.

Is there a simple example of the inline namespace in modern C++?

Here is a simple example that uses inline namespace,

Is there a simple example of the inline namespace in modern C++?

Here is a full example how we can use inline namespace with templates,

For more information on this feature, see Inline namespaces Proposal document.

What Is The Inline Namespace Feature In Modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?