How it works...

Neither auto nor typename in C++ provides the ability to get a variable's type and create new types using that information. To better explain why you might want to do this, let's look at the following example:

template<typename FUNC>auto question(FUNC &&func){    auto x = func() + 10;    return x;}

We start our example with a function that takes any function as an input and returns the result of this function plus 10. We can then execute this function as follows:

short the_answer(){    return 32;}int main(void){    auto i = question(the_answer);    show_type(i);}

As shown in the preceding example, we pass the question() function a pointer to another function that returns short. On executing this function, we store the results and then we ...

Get Advanced C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.