1
1
# Workarounds
2
2
3
+ ##### Table of Contents
4
+ [ std::format()] ( #format )
5
+ [ Abbreviated Function Template Syntax] ( #abbreviated )
6
+
7
+ <a name =" format " />
3
8
## std::format()
4
9
As of today, no compiler [ supports] ( https://en.cppreference.com/w/cpp/compiler_support ) the C++20 ` <format> ` module yet.
5
10
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.
8
13
As a workaround, we recommend the [ ` {fmt} ` ] ( https://fmt.dev/ ) library,
9
14
a free and open source implementation of a superset of the now standardised ` <format> ` module.
10
15
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
+
11
20
Steps:
12
21
1 . Download the ` fmt ` source code
13
22
- 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:
32
41
3 . If all went well, ` #include <format> ` will then use the [ ` format ` ] ( format ) header in this directory,
33
42
which injects ` {fmt} ` 's functionality into the ` std ` namespace (technically not allowed, we know...),
34
43
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