Portability of size_t and ptrdiff_t on recent compilers
While doing routine maintenance on the waLBerla bridge for ESPResSo, I was confronted with portability issues with symbols std::size_t
and std::ptrdiff_t
. When the <cstddef>
header file (or any other of the few headers that are guaranteed to declare these two symbols) is not explicitly included, compilers have to rely on an indirect include from the other headers in the translation unit. Since header files typically don't make guarantees about which header files they indirectly include, it is difficult to predict when a translation unit will fail to compile due to a missing std::size_t
symbol.
To make things worse, header files like <array>
can make std::size_t
visible but not size_t
(the version without namespace). This can be really confusing to unsuspecting programmers, because even though size_t
comes from the deprecated <stddef.h>
header, <cstddef>
often includes <stddef.h>
, giving the false impression that std::size_t
and size_t
are interchangeable.
More details and a minimal working example are available in espressomd/espresso#4313. While I couldn't get waLBerla to fail a build by varying compiler versions, it will probably happen at some point in time. This portability issue surfaced while using an in-house re-implementation of the include-what-you-use tool, which compiles header files to detect missing includes.
We already replaced all occurrences of size_t
by std::size_t
and added the missing <cstddef>
includes where necessary in ESPResSo and one of its dependencies. If you think it is worth applying the same changes to the waLBerla codebase, please consider branch jngrad/walberla@size_t_namespace where all occurrences of size_t
and ptrdiff_t
were replaced with their namespaced versions via tooling and <cstddef>
was included where necessary. If the potential for merge conflicts is too high, there is an alternative in jngrad/walberla@size_t where typedefs are used to make the plain size_t
and ptrdiff_t
visible in most of the codebase indirectly via src/core/DataTypes.h
(files that didn't include this header were updated individually to use the namespaced versions).