mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-16 17:47:02 +03:00
std::format: Formatting library.
This commit is contained in:
38
CPP20.md
38
CPP20.md
@@ -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++
|
||||
|
||||
38
README.md
38
README.md
@@ -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++
|
||||
|
||||
Reference in New Issue
Block a user