Search icon CANCEL
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
C++ Fundamentals

You're reading from   C++ Fundamentals Hit the ground running with C++, the language that supports tech giants globally

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789801491
Length 350 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Antonio Mallia Antonio Mallia
Author Profile Icon Antonio Mallia
Antonio Mallia
Francesco Zoffoli Francesco Zoffoli
Author Profile Icon Francesco Zoffoli
Francesco Zoffoli
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

C++ Fundamentals
Preface
1. Getting Started FREE CHAPTER 2. Functions 3. Classes 4. Generic Programming and Templates 5. Standard Library Containers and Algorithms 6. Object-Oriented Programming 7. Appendix

Being Generic in Templates


So far, we have learned how the compiler can make our templated functions easier to use by automatically deducing the types used. The template code decides whether to accept a parameter as a value or a reference, and the compiler finds the type for us. But what do we do if we want to be agnostic regarding whether an argument is a value or a reference, and we want to work with it regardless?

An example would be std::invoke in C++17. std::invoke is a function that takes a function as the first argument, followed by a list of arguments, and calls the function with the arguments. For example:

void do_action(int, float, double);
double d = 1.5;
std::invoke(do_action, 1, 1.2f, d);

Similar examples would apply if you wanted to log before calling a function, or you wanted to execute the function in a different thread, such as std::async does.

Let's demystify the difference by using the following code:

struct PrintOnCopyOrMove {
  PrintOnCopyOrMove(std::string name) : _name...
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