std::format: Formatting library.

This commit is contained in:
Anthony Calandra
2024-10-14 21:21:58 -04:00
parent 5d60755a38
commit 65d08b8d53
2 changed files with 76 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ C++20 includes the following new language features:
C++20 includes the following new library features:
- [concepts library](#concepts-library)
- [formatting library](#formatting-library)
- [synchronized buffered outputstream](#synchronized-buffered-outputstream)
- [std::span](#stdspan)
- [bit operations](#bit-operations)
@@ -532,6 +533,43 @@ Concepts are also provided by the standard library for building more complicated
See also: [concepts](#concepts).
### Formatting library
Combine the simplicity of `printf` with the type-safety of `iostream`. Uses braces as placeholders, and supports custom formatting similar to printf-style specifiers.
```c++
std::format("{1} {0}", "world", "hello"); // == "hello world"
int x = 123;
std::string str = std::format("x: {}", x); // str == "x: 123"
// Format to an output iterator:
for (auto x : {1, 2, 3}) {
std::format_to(std::ostream_iterator<char>{std::cout, "\n"}, "{}", x);
}
```
To format custom types:
```c++
struct fraction {
int numerator;
int denominator;
};
template <>
struct std::formatter<fraction>
{
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const fraction& f, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{0:d}/{1:d}", f.numerator, f.denominator);
}
};
fraction f{1, 2};
std::format("{}", f); // == "1/2"
```
### Synchronized buffered outputstream
Buffers output operations for the wrapped output stream ensuring synchronization (i.e. no interleaving of output).
```c++

View File

@@ -23,6 +23,7 @@ C++20 includes the following new language features:
C++20 includes the following new library features:
- [concepts library](#concepts-library)
- [formatting library](#formatting-library)
- [synchronized buffered outputstream](#synchronized-buffered-outputstream)
- [std::span](#stdspan)
- [bit operations](#bit-operations)
@@ -636,6 +637,43 @@ Concepts are also provided by the standard library for building more complicated
See also: [concepts](#concepts).
### Formatting library
Combine the simplicity of `printf` with the type-safety of `iostream`. Uses braces as placeholders, and supports custom formatting similar to printf-style specifiers.
```c++
std::format("{1} {0}", "world", "hello"); // == "hello world"
int x = 123;
std::string str = std::format("x: {}", x); // str == "x: 123"
// Format to an output iterator:
for (auto x : {1, 2, 3}) {
std::format_to(std::ostream_iterator<char>{std::cout, "\n"}, "{}", x);
}
```
To format custom types:
```c++
struct fraction {
int numerator;
int denominator;
};
template <>
struct std::formatter<fraction>
{
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const fraction& f, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{0:d}/{1:d}", f.numerator, f.denominator);
}
};
fraction f{1, 2};
std::format("{}", f); // == "1/2"
```
### Synchronized buffered outputstream
Buffers output operations for the wrapped output stream ensuring synchronization (i.e. no interleaving of output).
```c++