Branch
Hash :
d19a73d0
Author :
Date :
2025-05-28T01:59:05
stddef-h: Make 'unreachable' usable in C++ mode. Reported by Pierre Ossman <ossman@cendio.se> at <https://savannah.gnu.org/bugs/?67152>. * m4/stddef_h.m4 (gl_STDDEF_H): Also test whether unreachable is defined by <stddef.h> in C++ mode. * lib/stddef.in.h: In C++ mode, include <utility> and either import 'unreachable' from the std namespace or define it as an inline function. * tests/test-stddef-h.c: Disable some tests in C++ mode. * tests/test-stddef-h-c++.cc: Perform nearly the same tests in C++ mode as in C mode. * tests/test-stddef-h-c++2.cc: Rename some variables. Disable the NULL test with clang on Windows. * tests/test-stddef-h-c++3.cc: New file. * modules/stddef-h-c++-tests (Files): Include it. (Makefile.am): Link test-stddef-h-c++ with test-stddef-h-c++3.o.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/* Test of <stddef.h> substitute in C++ mode.
Copyright (C) 2019-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#define GNULIB_NAMESPACE gnulib
#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 b2 = 1;
size_t c2 = 2;
#if !(defined __cplusplus && defined _MSC_VER)
/* 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));