microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
std_debug.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
7// std-interop-file: opt-in Debug<T> specializations for std::array/std::optional/std::variant/std::monostate.
8
57#include <array>
58#include <bitset>
59#include <cstddef>
60#include <functional>
61#include <microfmt/microfmt.hpp>
62
63namespace reloco {
64
69template <typename T, std::size_t N> struct Debug<std::array<T, N>> {
70 static void format(const std::array<T, N> &val, const sink &out) noexcept {
71 out.put('[');
72 bool is_first = true;
73 for (const auto &elem : val) {
74 if (!is_first) {
75 out.write(", ");
76 }
77 is_first = false;
79 }
80 out.put(']');
81 }
82};
83
89template <std::size_t N> struct Debug<std::bitset<N>> {
90 static void format(const std::bitset<N> &val, const sink &out) noexcept {
91 for (std::size_t i = N; i > 0; --i) {
92 out.put(val.test(i - 1) ? '1' : '0');
93 }
94 }
95};
96
102template <typename T> struct Debug<std::reference_wrapper<T>> {
103 static void format(const std::reference_wrapper<T> &val, const sink &out) noexcept {
104 microfmt::format_to(out, "{}", microfmt::as_debug(val.get()));
105 }
106};
107
114template <> struct Debug<std::byte> {
115 static void format(const std::byte &val, const sink &out) noexcept {
116 out.write("0x");
117 microfmt::detail::format_unsigned<microfmt::detail::radix::hex>(out, std::to_integer<unsigned>(val), false, 2);
118 }
119};
120
121} // namespace reloco
Core formatting primitives, sinks, and customization point.
RELOCO_CONSTEXPR20 void format_to(const sink &out, microfmt::string_view fmt, const Args &...args) noexcept
Definition microfmt.hpp:1766
constexpr auto as_debug(const T &value RELOCO_LIFETIMEBOUND) noexcept
Wraps value so it always renders through reloco's Debug<T> customization point when one exists,...
Definition microfmt.hpp:1702
Definition debug_float.hpp:36
static void format(const std::array< T, N > &val, const sink &out) noexcept
Definition std_debug.hpp:70
static void format(const std::bitset< N > &val, const sink &out) noexcept
Definition std_debug.hpp:90
static void format(const std::byte &val, const sink &out) noexcept
Definition std_debug.hpp:115
static void format(const std::reference_wrapper< T > &val, const sink &out) noexcept
Definition std_debug.hpp:103