CARB_FORMATTED_SIZE

Defined in carb/extras/StringSafe.h

CARB_FORMATTED_SIZE(size, 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 with va_start, formats the string with std::vsnprintf, and calls a Callable with the formatted string. This function reserves a stack buffer of the size requested by size 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) where p is a const char*, such as a lambda, functor or function pointer. Retaining and reading p after the Callable returns is undefined behavior. If an error occurs with std::vsnprintf then p will be <vsnprintf failed>, or if a heap buffer was required and allocation failed, then p 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().

  • size – The size of the stack buffer to reserve. If the formatted string will be larger than this size, a buffer will be created on the heap instead, and destroyed once the callable returns.