Formatting¶
The C++ Standard Library introduces std::formatter
to define custom formatting rules for specific types. In chesscxx
, most types provide their own std::formatter
specializations. This allows you to use these types directly with standard formatting functions such as std::format
, std::print
, and related utilities.
std::formatter Specializations¶
Specialization |
Supported format specifiers |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::formatter<std::optional<T>>¶
-
template<typename T>
struct formatter¶ - #include <base_formatter.h>
Formatter for
std::optional<T>
whereT
is achesscxx
type that supports formatting.The format string can optionally include a prefix, suffix, underlying formatting specifications for the contained type, and a default value to display when the optional does not contain a value.
Format string syntax:
prefix[underlying_specs]suffix?default_value
prefix
: Text inserted before the value[underlying_specs]
: Format specs passed to the formatter of the underlyingT
suffix
: Text inserted after the value?default_value
: Fallback value if the optional is empty
- Template Parameters:
T – A
chesscxx
type withstd::formatter<T>
defined.
Examples¶
#include <chesscxx/piece_type.h>
#include <optional>
#include <print>
auto main() -> int {
std::optional<chesscxx::PieceType> empty;
std::optional<chesscxx::PieceType> rook = chesscxx::PieceType::kRook;
std::println(R"("{}" "{}")", empty, rook);
std::println(R"("{:?foo}" "{:?foo}")", empty, rook);
std::println(R"("{:[u]}" "{:[u]}")", empty, rook);
std::println(R"("{:[l]}" "{:[l]}")", empty, rook);
std::println(R"("{:[l]?foo}" "{:[l]?foo}")", empty, rook);
std::println(R"("{:bar[]}" "{:bar[]}")", empty, rook);
std::println(R"("{:[]baz}" "{:[]baz}")", empty, rook);
std::println(R"("{:bar[]baz}" "{:bar[]baz}")", empty, rook);
std::println(R"("{:bar[]baz?foo}" "{:bar[]baz?foo}")", empty, rook);
std::println(R"("{:bar[u]baz?foo}" "{:bar[u]baz?foo}")", empty, rook);
std::println("e7xd8{:=[u]}", empty);
std::println("e7xd8{:=[u]}", rook);
}
Output:
"" "rook"
"foo" "rook"
"" "R"
"" "r"
"foo" "r"
"" "barrook"
"" "rookbaz"
"" "barrookbaz"
"foo" "barrookbaz"
"foo" "barRbaz"
e7xd8
e7xd8=R