Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Expert C++ - Second Edition

You're reading from  Expert C++ - Second Edition

Product type Book
Published in Aug 2023
Publisher Packt
ISBN-13 9781804617830
Pages 604 pages
Edition 2nd Edition
Languages
Authors (5):
Marcelo Guerra Hahn Marcelo Guerra Hahn
Profile icon Marcelo Guerra Hahn
Araks Tigranyan Araks Tigranyan
Profile icon Araks Tigranyan
John Asatryan John Asatryan
Profile icon John Asatryan
Vardan Grigoryan Vardan Grigoryan
Profile icon Vardan Grigoryan
Shunguang Wu Shunguang Wu
Profile icon Shunguang Wu
View More author details

Table of Contents (24) Chapters

Preface 1. Part 1:Under the Hood of C++ Programming
2. Chapter 1: Building C++ Applications 3. Chapter 2: Beyond Object-Oriented Programming 4. Chapter 3: Understanding and Designing Templates 5. Chapter 4: Template Meta Programming 6. Chapter 5: Memory Management and Smart Pointers 7. Part 2: Designing Robust and Efficient Applications
8. Chapter 6: Digging into Data Structures and Algorithms in STL 9. Chapter 7: Advanced Data Structures 10. Chapter 8: Functional Programming 11. Chapter 9: Concurrency and Multithreading 12. Chapter 10: Designing Concurrent Data Structures 13. Chapter 11: Designing World-Ready Applications 14. Chapter 12: Incorporating Design Patterns in C++ Applications 15. Chapter 13: Networking and Security 16. Chapter 14: Debugging and Testing 17. Chapter 15: Large-Scale Application Design 18. Part 3:C++ in the AI World
19. Chapter 16: Understanding and Using C++ in Machine Learning Tasks 20. Chapter 17: Using C++ in Data Science 21. Chapter 18: Designing and Implementing a Data Analysis Framework 22. Index 23. Other Books You May Enjoy

Optimizing algorithms using traits

Instead of talking about this topic in a generic abstract way, we will use a classic optimized copy example to show the usage of type traits. Consider the standard library algorithm known as copy, shown here:

template<typename It1, typename It2>It2 copy(It1 first, It1 last, It2 out);

Obviously, we can write a generic version of copy() for any iterator types—that is, It1 and It2 here. However, as explained by the authors of the Boost library, there are some circumstances where the copy operation can be performed by memcpy(). We can use memcpy() if all of the following conditions are satisfied:

  • Both types of iterators, It1 and It2, are pointers
  • It1 and It2 must point to the same type, except for const and volatile qualifiers
  • A trivial assignment operator must be provided by the type that It1 points to

Here, the trivial assignment operator means that the type is either a scalar type or that one of the following...

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 €14.99/month. Cancel anytime}