| 1 |
// { dg-do run { target c++20 } } |
| 2 |
|
| 3 |
#include <format> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
enum color { red, green, blue }; |
| 7 |
const char* color_names[] = { "red", "green", "blue" }; |
| 8 |
|
| 9 |
template<> struct std::formatter<color> : std::formatter<const char*> { |
| 10 |
auto format(color c, format_context& ctx) const { |
| 11 |
return formatter<const char*>::format(color_names[c], ctx); |
| 12 |
} |
| 13 |
}; |
| 14 |
|
| 15 |
struct err {}; |
| 16 |
|
| 17 |
void |
| 18 |
test_specializations() // [format.formatter.spec] |
| 19 |
{ |
| 20 |
std::string s0 = std::format("{}", 42); // OK, library-provided formatter |
| 21 |
VERIFY( s0 == "42" ); |
| 22 |
using Fi = std::format_context::formatter_type<int>; |
| 23 |
static_assert( std::is_default_constructible_v<Fi> ); |
| 24 |
static_assert( std::is_copy_constructible_v<Fi> ); |
| 25 |
static_assert( std::is_move_constructible_v<Fi> ); |
| 26 |
static_assert( std::is_copy_assignable_v<Fi> ); |
| 27 |
static_assert( std::is_move_assignable_v<Fi> ); |
| 28 |
|
| 29 |
#ifdef _GLIBCXX_USE_CHAR8_T |
| 30 |
// std::string s0 = std::format("{}", u8'a'); // error: disabled formatter |
| 31 |
using Fc8 = std::format_context::formatter_type<char8_t>; |
| 32 |
static_assert( ! std::is_default_constructible_v<Fc8> ); |
| 33 |
static_assert( ! std::is_copy_constructible_v<Fc8> ); |
| 34 |
static_assert( ! std::is_move_constructible_v<Fc8> ); |
| 35 |
static_assert( ! std::is_copy_assignable_v<Fc8> ); |
| 36 |
static_assert( ! std::is_move_assignable_v<Fc8> ); |
| 37 |
#endif |
| 38 |
|
| 39 |
// std::string s1 = std::format("{}", L"foo"); // error: disabled formatter |
| 40 |
using Fw = std::format_context::formatter_type<wchar_t>; |
| 41 |
static_assert( ! std::is_default_constructible_v<Fw> ); |
| 42 |
static_assert( ! std::is_copy_constructible_v<Fw> ); |
| 43 |
static_assert( ! std::is_move_constructible_v<Fw> ); |
| 44 |
static_assert( ! std::is_copy_assignable_v<Fw> ); |
| 45 |
static_assert( ! std::is_move_assignable_v<Fw> ); |
| 46 |
|
| 47 |
using Fic = std::format_context::formatter_type<const int>; // disabled |
| 48 |
static_assert( ! std::is_default_constructible_v<Fic> ); |
| 49 |
static_assert( ! std::is_copy_constructible_v<Fic> ); |
| 50 |
static_assert( ! std::is_move_constructible_v<Fic> ); |
| 51 |
static_assert( ! std::is_copy_assignable_v<Fic> ); |
| 52 |
static_assert( ! std::is_move_assignable_v<Fic> ); |
| 53 |
|
| 54 |
std::string s2 = std::format("{}", red); // OK, user-provided formatter |
| 55 |
VERIFY( s2 == "red" ); |
| 56 |
using Fc = std::format_context::formatter_type<color>; |
| 57 |
static_assert( std::is_default_constructible_v<Fc> ); |
| 58 |
static_assert( std::is_copy_constructible_v<Fc> ); |
| 59 |
static_assert( std::is_move_constructible_v<Fc> ); |
| 60 |
static_assert( std::is_copy_assignable_v<Fc> ); |
| 61 |
static_assert( std::is_move_assignable_v<Fc> ); |
| 62 |
|
| 63 |
// std::string s3 = std::format("{}", err{}); // error: disabled formatter |
| 64 |
using Ferr = std::format_context::formatter_type<err>; |
| 65 |
static_assert( ! std::is_default_constructible_v<Ferr> ); |
| 66 |
static_assert( ! std::is_copy_constructible_v<Ferr> ); |
| 67 |
static_assert( ! std::is_move_constructible_v<Ferr> ); |
| 68 |
static_assert( ! std::is_copy_assignable_v<Ferr> ); |
| 69 |
static_assert( ! std::is_move_assignable_v<Ferr> ); |
| 70 |
|
| 71 |
// LWG 3833. Remove specialization |
| 72 |
// template<size_t N> struct formatter<const charT[N], charT> |
| 73 |
// Formatter is only expected to be instantiated with only cv-unqual types |
| 74 |
// and attempting to instantiate this specialization is ill-formed |
| 75 |
// using Farr = std::format_context::formatter_type<const char[1]>; |
| 76 |
// static_assert( ! std::is_default_constructible_v<Farr> ); |
| 77 |
// static_assert( ! std::is_copy_constructible_v<Farr> ); |
| 78 |
// static_assert( ! std::is_move_constructible_v<Farr> ); |
| 79 |
// static_assert( ! std::is_copy_assignable_v<Farr> ); |
| 80 |
// static_assert( ! std::is_move_assignable_v<Farr> ); |
| 81 |
} |
| 82 |
|
| 83 |
int main() |
| 84 |
{ |
| 85 |
test_specializations(); |
| 86 |
} |