:
struct S { int value; };
template<> struct std::formatter<S> {
size_t width_arg_id = 0;
constexpr auto parse(format_parse_context& ctx) {
auto iter = ctx.begin();
auto get_char = [&]() { return iter != ctx.end() ? *iter : 0; };
if (get_char() != '{')
return iter;
++iter;
char c = get_char();
if (!isdigit(c) || (++iter, get_char()) != '}')
throw format_error("invalid format");
width_arg_id = c - '0';
ctx.check_arg_id(width_arg_id);
return ++iter;
}
auto format(S s, format_context& ctx) {
int width = visit_format_arg([](auto value) -> int {
if constexpr (!is_integral_v<decltype(value)>)
throw format_error("width is not integral");
else if (value < 0 || value > numeric_limits<int>::max())
throw format_error("invalid width");
else
return value;
}, ctx.arg(width_arg_id));
return format_to(ctx.out(), "{0:x<{1}}", s.value, width);
}
};
std::string s = std::format("{0:{1}}", S{42}, 10);
—
end example