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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access