summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/std/format/formatter/concept.cc
blob: fe56dc44a68628642d1d7cf6b446677eebcee884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// { dg-options "-std=gnu++23" }
// { dg-do compile { target c++23 } }

#include <format>

struct S { };

template<> struct std::formatter<S> : std::formatter<const char*> {
  template<class Out>
  auto format(S, std::basic_format_context<Out, char>& ctx) const {
    return formatter<const char*>::format("ess", ctx);
  }
};

struct T { };

template<> struct std::formatter<T> : std::formatter<const char*> {
  // This function only accepts std::format_context, not other contexts.
  auto format(T, std::format_context& ctx) const {
    return formatter<const char*>::format("tee", ctx);
  }
};

struct U { };

void
test_concept() // [format.formattable]
{
  static_assert( std::formattable<int, char> );
  static_assert( std::formattable<const int, char> );
  static_assert( std::formattable<int, wchar_t> );
  static_assert( std::formattable<const int, wchar_t> );
  static_assert( std::formattable<char, char> );
  static_assert( std::formattable<char*, char> );
  static_assert( std::formattable<wchar_t, wchar_t> );
  static_assert( std::formattable<wchar_t*, wchar_t> );
  static_assert( std::formattable<char, wchar_t> );
  static_assert( ! std::formattable<char*, wchar_t> );
  static_assert( ! std::formattable<wchar_t, char> );
  static_assert( ! std::formattable<wchar_t*, char> );
  static_assert( std::formattable<S, char> );
  static_assert( std::formattable<const S, char> );
  static_assert( ! std::formattable<S, wchar_t> ); // only formats as char
  static_assert( ! std::formattable<T, char> ); // formatter not generic
  static_assert( ! std::formattable<U, char> ); // no formatter
}