Skip to content

Commit dfcc3ed

Browse files
Update README.md
1 parent 23715a0 commit dfcc3ed

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

Workarounds/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Workarounds
22

3+
##### Table of Contents
4+
[std::format()](#format)
5+
[Abbreviated Function Template Syntax](#abbreviated)
6+
7+
<a name="format"/>
38
## std::format()
49
As of today, no compiler [supports](https://en.cppreference.com/w/cpp/compiler_support) the C++20 `<format>` module yet.
510
This module provides safe, elegant, and efficient text formatting, primeraly in the form of the `std::format()` function,
@@ -8,6 +13,10 @@ and is heavily used throughout the C++20 edition of the book.
813
As a workaround, we recommend the [`{fmt}`](https://fmt.dev/) library,
914
a free and open source implementation of a superset of the now standardised `<format>` module.
1015

16+
Note: the workaround detailed below works for `#include <format>` directives;
17+
we have not had much luck getting this to work with C++20's `import` declarations.
18+
Until further notice, we therefore recommend you mostly use the `NoModules` versions of the source code.
19+
1120
Steps:
1221
1. Download the `fmt` source code
1322
- If you downloaded the book's source code as a zip file, you can download that of `fmt` as well from https://github.com/fmtlib/fmt,
@@ -32,5 +41,25 @@ Steps:
3241
3. If all went well, `#include <format>` will then use the [`format`](format) header in this directory,
3342
which injects `{fmt}`'s functionality into the `std` namespace (technically not allowed, we know...),
3443
and all `std::format()` statements will work as expected.
35-
36-
44+
45+
<a name="abbreviated"/>
46+
## Abbreviated Function Template Syntax
47+
48+
If your compiler does not [support](https://en.cppreference.com/w/cpp/compiler_support)
49+
C++20's abbreviated function template syntax yet, the solution is somewhat obvious:
50+
use the non-abbreviated syntax. That is, instead of
51+
52+
namespace math
53+
{
54+
auto square(const auto& x) { return x * x; }
55+
}
56+
57+
you use
58+
59+
namespace math
60+
{
61+
template <typename T>
62+
auto square(const T& x) { return x * x; }
63+
}
64+
65+
(`auto` return type deduction should work with any recent compiler.)

0 commit comments

Comments
 (0)