Skip to content

Commit bd2eace

Browse files
committed
compat/c23: improved
- for bool, nullptr compat, the comparison to 202311L should be <, not <= - separate bool/nullptr compat for clarity - improved countof compat - if c23, check stdcountof.h presence with __has_include and use if present rather than defining the macro always
1 parent 182119d commit bd2eace

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/compat/c23.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,30 @@
4141
#ifndef COMPAT_C23_H_B55FDBEB_BED9_4D10_85D8_BE8CE8C21997
4242
#define COMPAT_C23_H_B55FDBEB_BED9_4D10_85D8_BE8CE8C21997
4343

44+
#if !defined __cplusplus
45+
4446
// IWYU pragma: begin_exports
4547

46-
#if !defined __cplusplus && __STDC_VERSION__ <= 202311L
47-
#include <stddef.h>
48-
#define nullptr NULL
48+
// bool, true, false
49+
#if __STDC_VERSION__ < 202311L
50+
#include <stdbool.h>
4951
#endif
5052

51-
#include <stdbool.h> // for true, bool
53+
// nullptr
54+
#if __STDC_VERSION__ < 202311L
55+
#include <stddef.h>
56+
#define nullptr NULL
57+
#endif
5258

53-
#ifndef countof // defined in C2Y stdcountof.h
54-
#define countof(arr) (sizeof (arr) / sizeof (arr)[0])
59+
// countof
60+
#if __STDC_VERSION__ >= 202311L && __has_include(<stdcountof.h>)
61+
#include <stdcountof.h>
62+
#elif !defined countof
63+
#define countof(arr) (sizeof(arr) / sizeof(arr)[0])
5564
#endif
5665

5766
// IWYU pragma: end_exports
5867

68+
#endif // ! defined __cplusplus
5969
#endif // ! defined COMPAT_C23_H_B55FDBEB_BED9_4D10_85D8_BE8CE8C21997
6070

0 commit comments

Comments
 (0)