Site icon Learn C++

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.

[crayon-6763a93be4379307829733/]

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)

[crayon-6763a93be4383476551938/]

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

[crayon-6763a93be4386361396522/]

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,

[crayon-6763a93be4388770050405/]

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,

[crayon-6763a93be438a866353075/]

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

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.

Exit mobile version