Consider the TooBig functor in Listing 16.15.What does the following code do, and what values get assigned

Question:

Consider the TooBig functor in Listing 16.15.What does the following code do, and what values get assigned to bo?  

bool bo = TooBig(10)(15);

One functor (f100) is a declared object, and the second (TooBig(200)) is an
anonymous object created by a constructor call. Here’s the output of the program in
Listing 16.15:
Original lists:
50 100 90 180 60 210 415 88 188 201
50 100 90 180 60 210 415 88 188 201
Trimmed lists:
50 100 90 60 88
50 100 90 180 60 88 188
Suppose that you already have a template function with two arguments:
template
bool tooBig(const T & val, const T & lim)
{
return val > lim;
}

You can use a class to convert it to a one-argument function object:
template
class TooBig2
{
private:
T cutoff;
public:
TooBig2(const T & t) : cutoff(t) {}
bool operator()(const T & v) { return tooBig(v, cutoff); }
};
That is, you can use the following:
TooBig2 tB100(100);
int x;
cin >> x;
if (tB100(x)) // same as if (tooBig(x,100))
...
So the call tB100(x) is the same as tooBig(x,100), but the two-argument function is
converted to a one-argument function object, with the second argument being used to
construct the function object. In short, the class functor TooBig2 is a function adapter that
adapts a function to meet a different interface.
As noted in the listing, C++11’s initializer-list feature simplifies initialization.You
can replace
int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
list yadayada(vals, vals + 10); // range constructor
list etcetera(vals, vals + 10);
with this:
list yadayada = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
list etcetera {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  answer-question

C++ Primer Plus

ISBN: 9780321776402

6th Edition

Authors: Stephen Prata

Question Posted: