This patch makes it clear what one can expect from the <cstddef> header in C++ mode.
2025-05-25 Bruno Haible <br...@clisp.org> stddef-h tests: Add more C++ tests. * tests/test-stddef-h-c++2.cc: Test the expected contents of <cstddef>. diff --git a/tests/test-stddef-h-c++2.cc b/tests/test-stddef-h-c++2.cc index d2588e1227..e207976d13 100644 --- a/tests/test-stddef-h-c++2.cc +++ b/tests/test-stddef-h-c++2.cc @@ -18,3 +18,31 @@ #include <config.h> #include <cstddef> + +/* Test the expected contents of <cstddef>, cf. + <https://en.cppreference.com/w/cpp/header/cstddef>. */ + +/* Check that appropriate types are defined. */ +ptrdiff_t b = 1; +size_t c = 2; + +#if !defined __cplusplus || defined __GNUC__ || defined __clang__ +/* Check that NULL can be passed through varargs as a pointer type, + per POSIX 2008. */ +static_assert (sizeof NULL == sizeof (void *)); +#endif + +/* Check that offsetof produces integer constants with correct type. */ +struct d +{ + char e; + char f; +}; +static_assert (sizeof (offsetof (struct d, e)) == sizeof (size_t)); +static_assert (offsetof (struct d, f) == 1); + +#include <limits.h> /* INT_MAX */ + +/* offsetof promotes to an unsigned integer if and only if sizes do + not fit in int. */ +static_assert ((offsetof (struct d, e) < -1) == (INT_MAX < (size_t) -1));