Debian Codegraph @

libcxx/test/std/utilities/format/format.functions/bug_81590.compile.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9
10 // XFAIL: availability-fp_to_chars-missing
11
12 // The sample code is based on the bug report
13 // https://github.com/llvm/llvm-project/issues/81590
14 //
15 // Tests whether this formatter does not fail to compile due to nested concept
16 // evaluation.
17
18 #include <format>
19 #include <variant>
20
21 struct X : std::variant<X*> {
22 X* p = nullptr;
23 constexpr const std::variant<X*>& decay() const noexcept { return *this; }
24 };
25
26 template <>
27 struct std::formatter<X, char> : std::formatter<std::string, char> {
28 static constexpr auto format(const X& x, auto& ctx) {
29 if (!x.p)
30 return ctx.out();
31 auto m = [&](const X* t) { return std::format_to(ctx.out(), "{}", *t); };
32 return std::visit(m, x.decay());
33 }
34 };
35
36 void bug_81590() { (void)std::format("{}", X{}); }