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?