| 1 |
// { dg-do run { target c++20 } } |
| 2 |
|
| 3 |
// { dg-warning "deprecated" "std::visit_format_arg" { target c++26 } 0 } |
| 4 |
|
| 5 |
#include <format> |
| 6 |
#include <testsuite_hooks.h> |
| 7 |
|
| 8 |
// LWG 4106. basic_format_args should not be default-constructible |
| 9 |
static_assert( ! std::is_default_constructible_v<std::format_args> ); |
| 10 |
static_assert( ! std::is_default_constructible_v<std::wformat_args> ); |
| 11 |
|
| 12 |
template<typename Ctx, typename T> |
| 13 |
bool equals(std::basic_format_arg<Ctx> fmt_arg, T expected) { |
| 14 |
return std::visit_format_arg([=](auto arg_val) { |
| 15 |
if constexpr (std::is_same_v<decltype(arg_val), T>) |
| 16 |
return arg_val == expected; |
| 17 |
else |
| 18 |
return false; |
| 19 |
}, fmt_arg); |
| 20 |
} |
| 21 |
|
| 22 |
void |
| 23 |
test_empty() |
| 24 |
{ |
| 25 |
std::format_args args = std::make_format_args(); |
| 26 |
VERIFY(!args.get(0)); |
| 27 |
VERIFY(!args.get(1)); |
| 28 |
VERIFY(!args.get((std::size_t)-1)); |
| 29 |
VERIFY(equals(args.get(0), std::monostate{})); |
| 30 |
|
| 31 |
std::format_args cargs = std::make_format_args<std::format_context>(); |
| 32 |
VERIFY(!cargs.get(0)); |
| 33 |
VERIFY(equals(cargs.get(0), std::monostate{})); |
| 34 |
|
| 35 |
std::wformat_args wargs = std::make_wformat_args(); |
| 36 |
VERIFY(!wargs.get(0)); |
| 37 |
VERIFY(equals(wargs.get(0), std::monostate{})); |
| 38 |
} |
| 39 |
|
| 40 |
enum E { ByGum }; |
| 41 |
|
| 42 |
template<> |
| 43 |
struct std::formatter<E> : std::formatter<int> |
| 44 |
{ |
| 45 |
using std::formatter<int>::parse; |
| 46 |
|
| 47 |
std::format_context::iterator |
| 48 |
format(E e, std::format_context& fc) const |
| 49 |
{ return std::formatter<int>::format((int)e, fc); } |
| 50 |
}; |
| 51 |
|
| 52 |
void |
| 53 |
test_args() |
| 54 |
{ |
| 55 |
bool b = false; |
| 56 |
int i = 1; |
| 57 |
char c = '2'; |
| 58 |
double d = 3.4; |
| 59 |
|
| 60 |
// We need an lvalue of type format-arg-store<Context, Args...> for the |
| 61 |
// lvalue args to point to, otherwise it will have a dangling pointer. |
| 62 |
// This is not how you're supposed to use format args, just for testing. |
| 63 |
auto store = std::make_format_args(b, i, c, d); |
| 64 |
std::format_args args = store; |
| 65 |
VERIFY(equals(args.get(0), false)); |
| 66 |
VERIFY(equals(args.get(1), 1)); |
| 67 |
VERIFY(equals(args.get(2), '2')); |
| 68 |
VERIFY(equals(args.get(3), 3.4)); |
| 69 |
VERIFY(!args.get(4)); |
| 70 |
|
| 71 |
long l = 5L; |
| 72 |
unsigned long long ull = 6ULL; |
| 73 |
float f = 7.8f; |
| 74 |
|
| 75 |
auto cstore = std::make_format_args<std::format_context>(l, ull, f); |
| 76 |
std::format_args cargs = cstore; |
| 77 |
if constexpr (sizeof(long) == sizeof(int)) |
| 78 |
VERIFY(equals(cargs.get(0), 5)); |
| 79 |
else |
| 80 |
VERIFY(equals(cargs.get(0), 5LL)); |
| 81 |
VERIFY(equals(cargs.get(1), 6ULL)); |
| 82 |
VERIFY(equals(cargs.get(2), 7.8f)); |
| 83 |
VERIFY(!cargs.get(3)); |
| 84 |
|
| 85 |
std::string s = "tenfour"; |
| 86 |
VERIFY(equals(std::format_args(std::make_format_args(s)).get(0), std::string_view("tenfour"))); |
| 87 |
|
| 88 |
char nine = '9'; |
| 89 |
wchar_t ten = L'X'; |
| 90 |
// This needs to be on the stack so that testing pointer equality works. |
| 91 |
wchar_t eleven[] = L"eleven"; |
| 92 |
long double twelve13 = 12.13L; |
| 93 |
std::wstring tenfour = L"tenfour"; |
| 94 |
|
| 95 |
auto wstore = std::make_wformat_args(nine, ten, eleven, twelve13, tenfour); |
| 96 |
std::wformat_args wargs = wstore; |
| 97 |
VERIFY(equals(wargs.get(0), static_cast<wchar_t>('9'))); |
| 98 |
VERIFY(equals(wargs.get(1), L'X')); |
| 99 |
VERIFY(equals(wargs.get(2), static_cast<const wchar_t*>(eleven))); |
| 100 |
VERIFY(equals(wargs.get(3), 12.13L)); |
| 101 |
VERIFY(equals(wargs.get(4), std::wstring_view(tenfour))); |
| 102 |
VERIFY(!wargs.get(5)); |
| 103 |
|
| 104 |
std::nullptr_t null; |
| 105 |
E eebygum = E::ByGum; |
| 106 |
auto another_store = std::make_format_args(null, eebygum); |
| 107 |
args = another_store; |
| 108 |
VERIFY(equals(args.get(0), static_cast<const void*>(nullptr))); |
| 109 |
using handle = std::basic_format_arg<std::format_context>::handle; |
| 110 |
auto is_handle = []<typename T>(T) { return std::is_same_v<T, handle>; }; |
| 111 |
VERIFY(std::visit_format_arg(is_handle, args.get(1))); |
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
test_member_visit() |
| 116 |
{ |
| 117 |
#if __cpp_lib_format >= 202306L // C++26 adds std::basic_format_arg::visit |
| 118 |
int one = 1; |
| 119 |
float two = 2.0; |
| 120 |
std::string_view three = "three"; |
| 121 |
auto store = std::make_format_args(one, two, three); // See comment above. |
| 122 |
std::format_args args = store; |
| 123 |
auto a1 = args.get(0).visit([=](auto a) { |
| 124 |
if constexpr (std::is_same_v<decltype(a), int>) |
| 125 |
return a == one; |
| 126 |
return false; |
| 127 |
}); |
| 128 |
VERIFY( a1 ); |
| 129 |
auto a2 = args.get(1).visit([=](auto a) { |
| 130 |
if constexpr (std::is_same_v<decltype(a), float>) |
| 131 |
return a == two; |
| 132 |
return false; |
| 133 |
}); |
| 134 |
VERIFY( a2 ); |
| 135 |
auto a3 = args.get(2).visit([=](auto a) { |
| 136 |
if constexpr (std::is_same_v<decltype(a), std::string_view>) |
| 137 |
return a == three; |
| 138 |
return false; |
| 139 |
}); |
| 140 |
VERIFY( a3 ); |
| 141 |
|
| 142 |
auto a4 = args.get(0).visit<std::string_view>([](auto) { return "str"; }); |
| 143 |
static_assert( std::is_same_v<decltype(a4), std::string_view> ); |
| 144 |
VERIFY( a4 == "str" ); |
| 145 |
args.get(0).visit<void>([](auto){}); |
| 146 |
using V = decltype(args.get(0).visit<void>([](auto){})); |
| 147 |
static_assert( std::is_same_v<V, void> ); |
| 148 |
#endif |
| 149 |
} |
| 150 |
|
| 151 |
template<typename T> |
| 152 |
void test_visited_as_handle() |
| 153 |
{ |
| 154 |
T v{}; |
| 155 |
auto store = std::make_format_args(v); |
| 156 |
std::format_args args = store; |
| 157 |
|
| 158 |
constexpr auto is_handle = [](auto arg) { |
| 159 |
return std::is_same_v<decltype(arg), decltype(args.get(0))::handle>; |
| 160 |
}; |
| 161 |
VERIFY( std::visit_format_arg(is_handle, args.get(0)) ); |
| 162 |
#if __cpp_lib_format >= 202306L // C++26 adds std::basic_format_arg::visit |
| 163 |
VERIFY( args.get(0).visit(is_handle) ); |
| 164 |
#endif |
| 165 |
} |
| 166 |
|
| 167 |
template<typename E, typename S> |
| 168 |
void test_visited_as() |
| 169 |
{ |
| 170 |
auto v = static_cast<S>(1.0); |
| 171 |
auto store = std::make_format_args(v); |
| 172 |
std::format_args args = store; |
| 173 |
|
| 174 |
auto is_expected_val = [v](auto arg) { |
| 175 |
if constexpr (std::is_same_v<decltype(arg), E>) |
| 176 |
return arg == static_cast<E>(v); |
| 177 |
return false; |
| 178 |
}; |
| 179 |
VERIFY( std::visit_format_arg(is_expected_val, args.get(0)) ); |
| 180 |
#if __cpp_lib_format >= 202306L // C++26 adds std::basic_format_arg::visit |
| 181 |
VERIFY( args.get(0).visit(is_expected_val) ); |
| 182 |
#endif |
| 183 |
} |
| 184 |
|
| 185 |
template<typename T> |
| 186 |
concept can_format = std::is_default_constructible_v<std::formatter<T, char>>; |
| 187 |
|
| 188 |
int main() |
| 189 |
{ |
| 190 |
test_empty(); |
| 191 |
test_args(); |
| 192 |
test_member_visit(); |
| 193 |
|
| 194 |
#ifdef __SIZEOF_INT128__ |
| 195 |
test_visited_as_handle<__int128>(); |
| 196 |
test_visited_as_handle<unsigned __int128>(); |
| 197 |
#endif |
| 198 |
// TODO: This should be visited as handle. |
| 199 |
#ifdef __STDCPP_FLOAT16_T__ |
| 200 |
if constexpr (can_format<_Float16>) |
| 201 |
test_visited_as<float, _Float16>(); |
| 202 |
#endif |
| 203 |
#ifdef __STDCPP_BFLOAT16_T__ |
| 204 |
if constexpr (can_format<__gnu_cxx::__bfloat16_t>) |
| 205 |
test_visited_as<float, __gnu_cxx::__bfloat16_t>(); |
| 206 |
#endif |
| 207 |
#ifdef __FLT32_DIG__ |
| 208 |
if constexpr (can_format<_Float32>) |
| 209 |
test_visited_as<float, _Float32>(); |
| 210 |
#endif |
| 211 |
#ifdef __FLT64_DIG__ |
| 212 |
if constexpr (can_format<_Float64>) |
| 213 |
test_visited_as<double, _Float64>(); |
| 214 |
#endif |
| 215 |
#ifdef __FLT128_DIG__ |
| 216 |
if constexpr (can_format<_Float128>) |
| 217 |
# ifdef _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128 |
| 218 |
test_visited_as<long double, _Float128>(); |
| 219 |
# else |
| 220 |
test_visited_as_handle<_Float128>(); |
| 221 |
# endif |
| 222 |
#endif |
| 223 |
#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT |
| 224 |
if constexpr (!std::is_same_v<__ieee128, long double>) |
| 225 |
test_visited_as_handle<__ieee128>(); |
| 226 |
if constexpr (!std::is_same_v<__ibm128, long double>) |
| 227 |
test_visited_as_handle<__ibm128>(); |
| 228 |
#endif |
| 229 |
} |