Further Options

Let’s look at a couple more things you can do using function. First, we don’t actually have to declare six function<double(double)> objects in Listing 18.8. Instead, we can use a temporary function<double(double)> object as an argument to the use_f() function:

typedef function<double(double)> fdd; // simplify the type declarationcout << use_f(y, fdd(dub)) << endl;   // create and initialize object to dubcout << use_f(y, fdd(square)) << endl;...

Second, Listing 18.8 adapts the second arguments in use_f() to match the formal parameter f. Another approach is to adapt the type of the formal parameter f to match the original arguments. This can be done by using a function wrapper object as the second parameter for the use_f() template ...

Get C++ Primer Plus 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.