Skip to content

Commit 2ef1b40

Browse files
committed
Replace the dummy arg from compound initializers in the C API with more macro magic
The dummy argument {fmt_int} was there as a workaround for MSVC and to avoid needing to rely on another GCC extension for zero-size arrays. Use macro magic similar to FMT_NARG() instead that replaces the problematic zero-size array with NULL. Signed-off-by: Ferdinand Bachmann <ferdinand.bachmann@yrlf.at>
1 parent 4e1b170 commit 2ef1b40

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

include/fmt/fmt-c.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ typedef enum {} fmt_signed_char;
134134
FMT_NARG_(dummy, ##__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, \
135135
4, 3, 2, 1, 0)
136136

137+
// select between two expressions depending on whether __VA_ARGS__ is empty
138+
// expands to e if __VA_ARGS__ is empty and n otherwise
139+
# define FMT_VA_SELECT(e, n, ...) \
140+
FMT_NARG_(dummy, ##__VA_ARGS__, n, n, n, n, n, n, n, n, n, n, n, n, n, n, \
141+
n, n, e)
142+
137143
# define FMT_MAP_0(...)
138144
# define FMT_MAP_1(f, a) f(a)
139145
# define FMT_MAP_2(f, a, b) f(a), f(b)
@@ -168,11 +174,12 @@ typedef enum {} fmt_signed_char;
168174
# define FMT_MAP(f, ...) \
169175
FMT_CAT(FMT_MAP_, FMT_NARG(__VA_ARGS__))(f, ##__VA_ARGS__)
170176

171-
# define fmt_format(buffer, size, fmt, ...) \
172-
fmt_vformat( \
173-
(buffer), (size), (fmt), \
174-
(fmt_arg[]){{fmt_int}, FMT_MAP(FMT_MAKE_ARG, ##__VA_ARGS__)} + 1, \
175-
FMT_NARG(__VA_ARGS__))
177+
# define fmt_format(buffer, size, fmt, ...) \
178+
fmt_vformat((buffer), (size), (fmt), \
179+
FMT_VA_SELECT( \
180+
NULL, ((fmt_arg[]){FMT_MAP(FMT_MAKE_ARG, ##__VA_ARGS__)}), \
181+
##__VA_ARGS__), \
182+
FMT_NARG(__VA_ARGS__))
176183

177184
#endif // __cplusplus
178185

0 commit comments

Comments
 (0)