tagval 0.2.0
Modern C++23 header-only library of tagged values (open/closed enumerations with metadata)
Loading...
Searching...
No Matches
format.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <tagval/base.hpp>
9
10#include <concepts>
11#include <format>
12#include <ranges>
13
14template <typename T>
15 requires std::derived_from<T, tagval::detail::TagValBaseTag>
16struct std::formatter<T, char> { // NOLINT(cert-dcl58-cpp)
17 static constexpr auto parse(const std::format_parse_context& ctx) {
18 const auto* it = ctx.begin();
19 if (it != ctx.end() && *it != '}') {
20 throw std::format_error{"tagval: only the default format spec is supported"};
21 }
22 return it;
23 }
24
25 template <typename FormatContext>
26 auto format(const T& v, FormatContext& ctx) const {
27 const auto code = v.code();
28 return std::ranges::copy(code, ctx.out()).out;
29 }
30};
CRTP HandleBase used by ClosedEnded and OpenEnded.
static constexpr auto parse(const std::format_parse_context &ctx)
Definition format.hpp:17
auto format(const T &v, FormatContext &ctx) const
Definition format.hpp:26