Last Update:
"C++17 In Detail" First Update
Table of Contents
I’m happy to announce that just a few days ago I updated the book! “C++17 In Detail” grew by 7 pages (up to 219), includes a few new examples, new feature descriptions and lots of “bug fixes”.
See what’s inside.
The Changes
Here’s the short version of the release notes:
- Added section about nested namespaces in the General Language Features chapter
For example how to compact code like:
namespace MyCompany {
namespace ProjectA {
namespace SubsystemX{
class A{
// ...
};
class B {
// ...
};
} // SubsystemX
} // ProjectA
} // MyCompany
- Added more information about
overloaded
pattern:
For example the two lines below uses two C++17’s features:
template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
Do you know what are the required features here?
- Added section about using statement in folding expressions in the Template chapter
- A useful example of std::visit with multiple variants, in the Variant chapter
- Improved “Enforcing Code Contracts With [[nodiscard]] chapter
- Improved “Refactoring with optional” chapter - added info about
std::variant
- Grammar, typos, formatting issues, rewording
The update also improved that sample chapter - “General Language Features”. You can download it at leanpub page.
Here’s the link to the book:
As mentioned above the variant chapter includes one more example of
std::visit
, here’s a concept that you might be interested in.
“Skipping” Overloads in std::visit
As you might already know, std::visit
is used to invoke an operation
on the currently active type in a given variant (or variants).
I described it in the blog post about Everything You Need to Know About std::variant from C++17.
Here’s an example that also used “overload/overloaded” pattern:
template<class... Ts>
struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
int main() {
std::variant<int, float, char> v1 { 's' };
std::variant<int, float, char> v2 { 10 };
std::visit(overloaded{
[](int a, int b) { },
[](int a, float b) { },
[](int a, char b) { },
[](float a, int b) { },
[](float a, float b) { },
[](float a, char b) { },
[](char a, int b) { },
[](char a, float b) { },
[](char a, char b) { }
}, v1, v2);
return 0;
}
The example has two variants, and std::visit
is invoked on those two
variables. The main issue here is that you have to provide all
combinations of binary operations.
Each variant has three possible types (or states), so we have 3*3 combinations.
However, how to specify only “interesting” overloads? Maybe some combinations are invalid, and maybe only a few do something meaningful?
It appears that you can use the concept of generic lambda to implement a “default” overload function!
For example:
std::visit(overloaded{
[](int a, int b) { },
[](int a, float b) { },
[](int a, char b) { },
[](float a, int b) { },
[](auto a, auto b) { }, // << default!
}, v1, v2);
In the example above you can see that only four overloads have specific types - let’s say those are the “valid” (or “meaningful”) overloads. The rest is handled by generic lambda (available since C++14).
Generic lambda resolves to a template function. It has less priority than a “concrete” function overloads when the compiler creates the final overload resolution set.
In the book (in this fresh book update) I show a bit better example, with more “practical” problem, and with more explanation.
The Plans
As you know the book is 90% ready, and here’s the general overview of what you can expect in the following months:
- string operations chapter: with string searchers and string conversions. As you’ve seen I’ve even started some experiments with searchers: here and here on the blog.
- rewrite the filesystem chapter
- rewrite the parallel stl chapter
- add more examples to the third part of the book
I hope to deliver the first three bullet points every three/four weeks.
Until the book is not 100% done, you have a chance to buy it much cheaper and get free updates later.
Your Feedback
I appreciate your initial feedback and support! The book has now more than 250 readers! That’s not too bad I think :)
Let me know what’s your experience with the book. What would you like to change? What would you like to see more?
The Book
Once again here’s the link to the book:
C++17 In Detail
@Leanpub
I've prepared a valuable bonus for you!
Learn all major features of recent C++ Standards on my Reference Cards!
Check it out here: