On Wed, Feb 21, 2024 at 04:26:52PM +0000, Jøger Hansegård via Development wrote:
>    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.]

The given reason there is outdated, but it's only the most obvious reason
at the time of its writing. There are more:

1. 'static' is attached to individual functions, not a scope of uncertain
extend. When working on unfamiliar code it helps to understand the context.
With 'static' the locality is obvious in the immediate context of the function
and not set by some 'namespace {' potentiall a dozen functions and hundreds of
lines of code away.

2. Anonymous namespaces typically require more lines of code / vertical space.

3. Anonymous namesoaces require extra conventions on whether (and if so, how)
to mark the closing of the namespace by comments ('} // anon' ?)

4. Anonymous namespaces are not anonymous, but effectively typically(?) only
oddly-enough-named-so-there-are-no-clashes.

5. It should be encouraged to hide functions as much as possible. This is
more work when using namespaces:
   5.1) identifying such functions is more difficult, since it's not clear
        from the function itself (see 1.) whether it is already hidden
   5,2) actual hiding a so-far-visible function is more effort:
        (add 'namespace {' and '}' and probably two or three empty lines
         vs typing 'static' once)
So chances are biased towards not hide the hideable function.

6. Even if the optimized final compiled product is identical, intermediate
steps are not:

  echo 'namespace { void foo() {} }' | gcc -xc++ - -g -S  -o - | wc
    156     320    2509
  echo 'static void foo() {}' | gcc -xc++ - -g -S  -o - | wc
    133     274    2154

Less lines, less characters, shorter symbols. This plays a role when it comes
to compile times and debugging.

7. Visible from 6.: Less typing for the occasional hidden function.

> Can we delete this statement and lean on Cpp Core Guidelines Cpp Core 
> Guidelines
> SF.22 instead 
> https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-unna
> med2?

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'

Andre'
-- 
Development mailing list
Development@qt-project.org
https://lists.qt-project.org/listinfo/development

Reply via email to