[Development] Can we remove recommendation against unnamed namespaces from Qt coding conventions?

Jøger Hansegård joger.hansegard at qt.io
Wed Feb 21 19:56:41 CET 2024


>>    Our Qt coding conventions ([1]https://wiki.qt.io/Coding_Conventions)
>>    has a statement on the use of unnamed (anonymous) namespaces. As far as
>>    I understand, this statement is now outdated.

> [I'll assume we are talking about functions here.]

Anonymous namespaces can be quite helpful to avoid name clashes of non-function types.
Quite often I see both local functions and other local entities in the same cpp
file. I don't want to recommend against moving local functions into the unnamed
namespace unless there is a technical reason why we shouldn't.

> It would be a change to the worse in my book which I personally would not like.
> Anonymous namespaces are harder to set up and to maintain and produce at best
> identical results compared to static'

Would an option be to change from:

   Avoid the use of anonymous namespaces in favor of the static keyword
   if possible. A name localized to the compilation unit with static is
   guaranteed to have internal linkage. For names declared in anonymous
   namespaces the C++ standard unfortunately mandates external linkage.
   (7.1.1/6, or see various discussions about this on the gcc mailing
   lists)

to

   Use unnamed namespaces for all internal/non-exported entities. Functions can
   alternatively be marked `static` in the global namespace. Marking functions 
   `static` also in the unnamed namespace can help readability, particularly in reviews.

--
Jøger


More information about the Development mailing list