Adaptors for Functions
The two functions std::bind
and std::functions
fit very well together. While std::bind
enables you to create new function objects on the fly, std::function
takes these temporary function objects and binds them to a variable. Both functions are powerful tools from functional programming and need the header <functional>
.
// bindAndFunction.cpp
...
#include
<functional>
...
// for placehoder _1 and _2
using
namespace
std
::
placeholders
;
f
using
std
::
bind
;
using
std
::
function
...
double
divMe
(
double
a
,
double
b
){
return
a
/
b
;
};
function
<
double
(
double
,
double
)
>
myDiv1
=
bind
(
divMe
,
_1
,
_2
);
function
<
double
(
double
)
>
myDiv2
=
bind
(
divMe
,
2000
,
_1
);
divMe
(
2000
,
10
)
==
myDiv1
(
2000
,
10
)
==
myDiv2
(
10
);