polymorphic function wrapper?

From my book:

-----------------------------------------------------------------------------
Naming a Lambda Expression

You can assign a stateless lambda — one that does not reference anything in an external scope — to a variable that is a function pointer, thus giving the lambda a name. You can then use the function pointer to call the lambda as many times as you like. For example:

auto sum = [](int a,int b){return a+b; };

The function pointer, sum, points to the lambda expression, so you can use it as many times as you like:

1
2
cout << "5 + 10 equals " << sum(5,10) << endl;
cout << "15 + 16 equals " << sum(15,16) << endl;


This is quite useful, but there’s a much more powerful capability. An extension to the STL functional header defi nes the function<> class template that you can use to define a wrapper for a function object; this includes a lambda expression. The function<> template is referred to as a polymorphic function wrapper because an instance of the template can wrap a variety of function
objects with a given parameter list and return type.

I won’t discuss all the details of the function<> class template, but I’ll just show how you can use it to wrap a lambda expression. Using the function<> template to wrap a lambda expression effectively gives a name to the lambda expression, which not only provides the possibility of recursion within a lambda expression, it also allows you to use the lambda expression in multiple statements or pass it to several different functions. The same function wrapper could also wrap different lambda expressions at different times.


-----------------------------------------------------------------------------

What does it mean by polymorphic function wrapper. I know what polymorphism is, but I dont exactly understand this.

Does it mean it uses virtual functions? If so how does it use virtual functions?
Last edited on
It uses parametric polymorphism
http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29#Parametric_polymorphism
That means that that function wrapper can be any type depending on function, its arguments and return type.
Before there were only unary_function and binary_function which could take one ot two arguments (and show Ad-hoc polymorphism)
Last edited on
So polymorhpic describes anything that includes a form of polymorphism right?

I am still slightly confused.
Last edited on
Also shouldn't it be called generic function wrapper?
Another quick question related to the Function Object:

from my book:

"You can use the function<> class template to specify the type for a function parameter (makes sense). This enables you to define a function with a parameter for which you can supply an argument that can be a function object, a lambda expression, or a normal function pointer. (dont exactly understand this)"

Can you please explain the second part?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <functional>
#include <iostream>

//Normal function
int getRandomNumber()
{
    return 4; // chosen by fair dice roll
              // guaranteed to be random
}

//Functor class
class Functor
{
public:
    void operator()(int x)
    {
        std::cout << x;
    }
};

int main()
{
    Functor show;

    //create function object from function pointer
    std::function<int ()> func_pointer(getRandomNumber);

    //create function object from functor object
    std::function<void (int)> functor(show);

    //create function from lambda expression
    std::function<void ()> lambda(
                                  [&](){ functor( func_pointer() ); }
                                  );

    //Calling lambda function object
    lambda();
}

Last edited on
Thanks just a question about line 7 and 8's comments.
How is it guaranteed to be random?

Two questions as well:

So polymorhpic describes anything that includes a form of polymorphism right?

Also shouldn't it be called generic function wrapper?

I am still slightly confused.
Anmol444 wrote:
Thanks just a question about line 7 and 8's comments.
How is it guaranteed to be random?
It was chosen by a fair dice roll: http://xkcd.com/221/
(It's a joke ;p)
Anmol444 wrote:
So polymorhpic describes anything that includes a form of polymorphism right?
right

Anmol444 wrote:
Also shouldn't it be called generic function wrapper?
Wikipedia wrote:
Parametric polymorphism is also available in several object-oriented languages, where it goes under the name "generics"


Anmol444 wrote:
How is it guaranteed to be random?
That was a joke
http://xkcd.com/221/
Oh lol fail by me sorry xD. Well thanks!
Last edited on
Topic archived. No new replies allowed.