diff options
| author | Markus Mittendrein <maxmitti@maxmitti.tk> | 2022-03-22 21:29:06 +0100 |
|---|---|---|
| committer | Markus Mittendrein <maxmitti@maxmitti.tk> | 2022-03-22 21:29:06 +0100 |
| commit | e7917bbdfbdcccdf5516910709f60f245830c59c (patch) | |
| tree | 6a96c79d8798fc1bde0908faead1b3c2e5e08466 /include | |
| parent | d3ee8accd4fde07fbe313767c19c715b8d4ef359 (diff) | |
| download | cxxformat-e7917bbdfbdcccdf5516910709f60f245830c59c.tar.gz cxxformat-e7917bbdfbdcccdf5516910709f60f245830c59c.zip | |
Diffstat (limited to 'include')
| -rw-r--r-- | include/cxxformat/core.hpp | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/include/cxxformat/core.hpp b/include/cxxformat/core.hpp index 662f4f8..43b3c41 100644 --- a/include/cxxformat/core.hpp +++ b/include/cxxformat/core.hpp @@ -486,33 +486,36 @@ namespace format { const auto out = make_output(std::forward<Output>(output)); const auto format_arg = [&args..., &out]<format_specifier spec>(holder<spec>) -> decltype(auto) { - optional_int<std::size_t> minWidth = spec.minWidth; - if constexpr (spec.flags.widthAsArg) + if constexpr (sizeof...(Args) > 0) { - using Arg = std::decay_t<nth_type<*spec.minWidth, Args...>>; - static_assert(std::unsigned_integral<Arg>, "Width argument must be an unsigned integral"); - static_assert(sizeof(Arg) <= sizeof(std::size_t), "Width argument type must be at most as big as std::size_t"); - minWidth = nth_argument<*spec.minWidth>(std::forward<Args>(args)...); + optional_int<std::size_t> minWidth = spec.minWidth; + if constexpr (spec.flags.widthAsArg) + { + using Arg = std::decay_t<nth_type<*spec.minWidth, Args...>>; + static_assert(std::unsigned_integral<Arg>, "Width argument must be an unsigned integral"); + static_assert(sizeof(Arg) <= sizeof(std::size_t), "Width argument type must be at most as big as std::size_t"); + minWidth = nth_argument<*spec.minWidth>(std::forward<Args>(args)...); + } + optional_int<std::size_t> precision = spec.precision; + if constexpr (spec.flags.precisionAsArg) + { + using Arg = std::decay_t<nth_type<*spec.precision, Args...>>; + static_assert(std::unsigned_integral<Arg>, "Precision argument must be an unsigned integral"); + static_assert(sizeof(Arg) <= sizeof(std::size_t), "Precision argument type must be at most as big as std::size_t"); + precision = nth_argument<*spec.precision>(std::forward<Args>(args)...); + } + + using Arg = nth_type<spec.argIndex, Args...>; + Arg&& arg = nth_argument<spec.argIndex>(std::forward<Args>(args)...); + using formatter = formatter_with_fallback<std::decay_t<Arg>>; + + []() consteval { + // throws on error + formatter::conversionSupported(spec); + }(); + + return formatter::format(out, std::forward<Arg>(arg), spec, minWidth, precision); } - optional_int<std::size_t> precision = spec.precision; - if constexpr (spec.flags.precisionAsArg) - { - using Arg = std::decay_t<nth_type<*spec.precision, Args...>>; - static_assert(std::unsigned_integral<Arg>, "Precision argument must be an unsigned integral"); - static_assert(sizeof(Arg) <= sizeof(std::size_t), "Precision argument type must be at most as big as std::size_t"); - precision = nth_argument<*spec.precision>(std::forward<Args>(args)...); - } - - using Arg = nth_type<spec.argIndex, Args...>; - Arg&& arg = nth_argument<spec.argIndex>(std::forward<Args>(args)...); - using formatter = formatter_with_fallback<std::decay_t<Arg>>; - - []() consteval { - // throws on error - formatter::conversionSupported(spec); - }(); - - return formatter::format(out, std::forward<Arg>(arg), spec, minWidth, precision); }; (([&]{ |
