Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Design Patterns with C++

You're reading from   Hands-On Design Patterns with C++ Solve common C++ problems with modern design patterns and build robust applications

Arrow left icon
Product type Paperback
Published in Jul 2023
Publisher Packt
ISBN-13 9781804611555
Length 626 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Fedor G. Pikus Fedor G. Pikus
Author Profile Icon Fedor G. Pikus
Fedor G. Pikus
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Part 1: Getting Started with C++ Features and Concepts
2. Chapter 1: An Introduction to Inheritance and Polymorphism FREE CHAPTER 3. Chapter 2: Class and Function Templates 4. Chapter 3: Memory and Ownership 5. Part 2: Common C++ Idioms
6. Chapter 4: Swap – from Simple to Subtle 7. Chapter 5: A Comprehensive Look at RAII 8. Chapter 6: Understanding Type Erasure 9. Chapter 7: SFINAE, Concepts, and Overload Resolution Management 10. Part 3: C++ Design Patterns
11. Chapter 8: The Curiously Recurring Template Pattern 12. Chapter 9: Named Arguments, Method Chaining, and the Builder Pattern 13. Chapter 10: Local Buffer Optimization 14. Chapter 11: ScopeGuard 15. Chapter 12: Friend Factory 16. Chapter 13: Virtual Constructors and Factories 17. Chapter 14: The Template Method Pattern and the Non-Virtual Idiom 18. Part 4: Advanced C++ Design Patterns
19. Chapter 15: Policy-Based Design 20. Chapter 16: Adapters and Decorators 21. Chapter 17: The Visitor Pattern and Multiple Dispatch 22. Chapter 18: Patterns for Concurrency 23. Assessments 24. Index 25. Other Books You May Enjoy

Visitor in C++17

C++17 introduced a major change in the way we use the Visitor pattern with the addition of std::variant to the standard library. The std::variant template is essentially a “smart union:” std::variant<T1, T2, T3> is similar to union { T1 v1; T2 v2; T3 v3; } in that both can store a value of one of the specified types and only one value can be stored at a time. The key difference is that a variant object knows which type it contains, while with a union the programmer is wholly responsible for reading the same type as what was written earlier. It is undefined behavior to access a union as a type that differs from the one used to initialize it:

union { int i; double d; std::string s; } u;
u.i = 0;
++u.i;               // OK
std::cout << u.d;     // Undefined behavior

In contrast, std::variant offers a safe way to store values of different...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime