Skip to content

Commit 65d08b8

Browse files
std::format: Formatting library.
1 parent 5d60755 commit 65d08b8

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

CPP20.md

+38
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ C++20 includes the following new language features:
2424

2525
C++20 includes the following new library features:
2626
- [concepts library](#concepts-library)
27+
- [formatting library](#formatting-library)
2728
- [synchronized buffered outputstream](#synchronized-buffered-outputstream)
2829
- [std::span](#stdspan)
2930
- [bit operations](#bit-operations)
@@ -532,6 +533,43 @@ Concepts are also provided by the standard library for building more complicated
532533

533534
See also: [concepts](#concepts).
534535

536+
### Formatting library
537+
Combine the simplicity of `printf` with the type-safety of `iostream`. Uses braces as placeholders, and supports custom formatting similar to printf-style specifiers.
538+
```c++
539+
std::format("{1} {0}", "world", "hello"); // == "hello world"
540+
541+
int x = 123;
542+
std::string str = std::format("x: {}", x); // str == "x: 123"
543+
544+
// Format to an output iterator:
545+
for (auto x : {1, 2, 3}) {
546+
std::format_to(std::ostream_iterator<char>{std::cout, "\n"}, "{}", x);
547+
}
548+
```
549+
550+
To format custom types:
551+
```c++
552+
struct fraction {
553+
int numerator;
554+
int denominator;
555+
};
556+
557+
template <>
558+
struct std::formatter<fraction>
559+
{
560+
constexpr auto parse(std::format_parse_context& ctx) {
561+
return ctx.begin();
562+
}
563+
564+
auto format(const fraction& f, std::format_context& ctx) const {
565+
return std::format_to(ctx.out(), "{0:d}/{1:d}", f.numerator, f.denominator);
566+
}
567+
};
568+
569+
fraction f{1, 2};
570+
std::format("{}", f); // == "1/2"
571+
```
572+
535573
### Synchronized buffered outputstream
536574
Buffers output operations for the wrapped output stream ensuring synchronization (i.e. no interleaving of output).
537575
```c++

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ C++20 includes the following new language features:
2323

2424
C++20 includes the following new library features:
2525
- [concepts library](#concepts-library)
26+
- [formatting library](#formatting-library)
2627
- [synchronized buffered outputstream](#synchronized-buffered-outputstream)
2728
- [std::span](#stdspan)
2829
- [bit operations](#bit-operations)
@@ -636,6 +637,43 @@ Concepts are also provided by the standard library for building more complicated
636637

637638
See also: [concepts](#concepts).
638639

640+
### Formatting library
641+
Combine the simplicity of `printf` with the type-safety of `iostream`. Uses braces as placeholders, and supports custom formatting similar to printf-style specifiers.
642+
```c++
643+
std::format("{1} {0}", "world", "hello"); // == "hello world"
644+
645+
int x = 123;
646+
std::string str = std::format("x: {}", x); // str == "x: 123"
647+
648+
// Format to an output iterator:
649+
for (auto x : {1, 2, 3}) {
650+
std::format_to(std::ostream_iterator<char>{std::cout, "\n"}, "{}", x);
651+
}
652+
```
653+
654+
To format custom types:
655+
```c++
656+
struct fraction {
657+
int numerator;
658+
int denominator;
659+
};
660+
661+
template <>
662+
struct std::formatter<fraction>
663+
{
664+
constexpr auto parse(std::format_parse_context& ctx) {
665+
return ctx.begin();
666+
}
667+
668+
auto format(const fraction& f, std::format_context& ctx) const {
669+
return std::format_to(ctx.out(), "{0:d}/{1:d}", f.numerator, f.denominator);
670+
}
671+
};
672+
673+
fraction f{1, 2};
674+
std::format("{}", f); // == "1/2"
675+
```
676+
639677
### Synchronized buffered outputstream
640678
Buffers output operations for the wrapped output stream ensuring synchronization (i.e. no interleaving of output).
641679
```c++

0 commit comments

Comments
 (0)