CARB_FORMATTED
Defined in carb/extras/StringSafe.h
-
CARB_FORMATTED(fmt, ...)
Formats a string as if by vsnprintf and invokes a callable with the result.
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. This function reserves a stack buffer of the size requested bysize
for CARB_FORMATTED_SIZE or a default size for CARB_FORMATTED, but if the formatted string will not fit into that buffer, will use the heap to create a larger buffer.See also
CARB_FORMATTED(), CARB_FORMATTED_SIZE(), CARB_FORMATTED_N(), CARB_FORMATTED_N_SIZE(), carb::extras::withFormatNV(), carb::extras::withFormatV()
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([&](const char* p) { log(p); }); }
- Parameters
fmt – The
printf
-style format string.... – A Callable that may be invoked as via
...(p)
wherep
is aconst char*
, 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 wish the callback to receive the length of the formatted string as well, use CARB_FORMATTED_N() or CARB_FORMATTED_N_SIZE().