qxLib
std_formatters.h
Go to the documentation of this file.
1 /**
2 
3  @file std_formatters.h
4  @details The content of the file extends std::formatter to let it support some std classes.
5  It may break a compilation if someone already did it or some of the realisations were added to std.
6  Include it on your own risk.
7  @author Khrapov
8  @date 26.08.2025
9  @copyright © Nick Khrapov, 2025. All right reserved.
10 
11 **/
12 #pragma once
13 
15 
16 #include <format>
17 #include <type_traits>
18 #include <variant>
19 
20 namespace qx
21 {
22 
23 template<class T, class char_t>
24 concept formattable_c = requires(T& value, std::basic_format_context<std::add_pointer_t<char_t>, char_t>& ctx) {
25  std::formatter<T, char_t>().format(value, ctx);
26 };
27 
28 } // namespace qx
29 
30 template<class char_t, class... args_t>
31 struct std::formatter<std::variant<args_t...>, char_t> : qx::basic_formatter<char_t>
32 {
33  template<class context_t>
34  constexpr auto format(const std::variant<args_t...>& variant, context_t& ctx) const
35  {
36  return std::visit(
37  [&ctx]<class T>(const T& value)
38  {
39  if constexpr (qx::formattable_c<T, char_t>)
40  return std::format_to(ctx.out(), QX_STR_PREFIX(char_t, "{}"), value);
41  else
42  QX_STATIC_ASSERT_NO_INSTANTIATION("No formatter for this type");
43  },
44  variant);
45  }
46 };
requires(same_variadic_args_v< args_t... >) const expr auto coalesce(args_t &&... args)
Coalesce function, C# a ?? b analogue.
Definition: coalesce.inl:57
Static assert macros.
#define QX_STR_PREFIX(value_t, str)
Chose witch of prefixes add to string : L or none.
Definition: string_utils.h:253