CARB_FORMATTED_N
Defined in carb/extras/StringSafe.h
-
CARB_FORMATTED_N(fmt, ...)
Formats a string as if by vsnprintf and invokes a callable with the result and the length.
This macro constructs a
va_list
, gathers the variadic parameters withva_start
, formats the string withstd::vsnprintf
, and calls a Callable with the formatted string and length. This function reserves a stack buffer of the size requested bysize
for CARB_FORMATTED_N_SIZE or a default size for CARB_FORMATTED_N, but if the formatted string will not fit into the stack buffer, will use the heap to create a larger buffer.Note
This macro is intended to be used in variadic functions as a simple means of replacing uses of
std::vsnprintf
.void myLogFunction(const char* fmt, ...) { CARB_FORMATTED_N([&](const char* p, size_t len) { log(p, len); }); }
- Parameters
fmt – The
printf
-style format string.... – A Callable that may be invoked as via
...(p, n)
(wherep
is aconst char*
andn
is asize_t
), such as a lambda, functor or function pointer. Retaining and readingp
after the Callable returns is undefined behavior. If an error occurs withstd::vsnprintf
thenp
will be<vsnprintf failed>
, or if a heap buffer was required and allocation failed, thenp
will be<failed to allocate>
. If you do not need the length provided to the Callable, use CARB_FORMATTED() or CARB_FORMATTED_SIZE() instead.