Namespaces represent a cross-file named scope. They are very useful to organize code and interfaces without cluttering a unique namespace. For
instance, they provide a much cleaner way to avoid name collisions than using bad long names.
Namespaces can be nested to provide even more structure to type and symbol names. In that case, namespaces can be nested one inside another like
scopes would with curly braces.
In C++17, a new concise syntax was introduced to increase the readability of nested namespaces. It is much less verbose and involves much less
curly braces-delimited scopes. Whereas declaring a nested namespace of depth N requires N pairs of curly braces with the original syntax, this new
syntax requires only one pair of curly braces. This syntax is much more readable and less error-prone. When possible, non-inlined or inlined (since
C++20) named namespaces should be concatenated.
Noncompliant code example
namespace geometry { // Noncompliant
namespace common {
class point {
};
}
}
namespace geometry { // Noncompliant since C++20
inline namespace triangle {
class edge {
};
}
}
Compliant solution
namespace geometry::common {
class point {
};
}
namespace geometry::inline triangle { // C++20
class edge {
};
}
namespace sonarsource { // Compliant: cannot be concatenated
namespace core {
class Rule {
};
}
class A {
};
}