??????????C++0x??е??????????????????C++0x????????lambda?????? function?????bind????????????????????????????????????????????????е?????????????????????????????????????????????????????closure?????????????????????lambda?????????????????????wikipedia??????????????closure????壺
????A closure (also lexical closure?? function closure or function value) is a function together with
????a referencing environment for the non-local variables of that function.
?????????????????closure????????????????????????????????????????????????????????????closure??????????????巶Χ????????????????????non-local vriables?????????????????????????closure?????????????????????????????????function?? bind??lambda????????????????????????????????????????и????????????????????????????????????????
????1. function
???????????????C++?У??????????????????????????????????????????????????????????????????????opetator()???????C++98?е?functor)??C++0x?У????????????std::function????std::function???????C++?????е?????????????????????????????????????????????????壬???????????????????????????????function??????????
#include < functional>
std::function< size_t (const char*) > print_func;
/// normal function -> std::function object
size_t CPrint(const char*) { ... }
print_func = CPrint;
print_func("hello world"):
/// functor -> std::function object
class CxxPrint
{
public:
size_t operator()(const char*) { ... }
};
CxxPrint p;
print_func = p;
print_func("hello world");
????????????????У??????????????????????functor??????????std::function????????????????????????á???????C++?е???????嶼?????????????????á????std::function?????????????????????????????????????????壬??????y?????????????????????std::function??????÷????????????????Щ??ù????е????????
??????1?????????????????std::function??????????????????????
????a. ??????std::function??????????????????????????
????b. ???????????????????std::function????????????????е???????????????????void??std::function????????????????
??????2??std::function???????refer to????1?????????????????????
??????3??std::function object????????????????????????????????????????????????????????
????2. bind
????bind????????????????????????????????????Щ???????????е??????????????μ???????壬???????????????????ù????????????á?C++98?У???????????bind1st??bind2nd?????????????????functor????????????????????????????????????????????????????bind1st??bind2nd????????????C++0x?У?????std::bind??????????????????????????????????Щ???????????????????????????bind??????????????????????????bind1st??bind2nd???????????????C++0x?в???????bind1st??bind2nd???????deprecated?????????????????????????bind???÷???
#include < functional>
int Func(int x?? int y);
auto bf1 = std::bind(Func?? 10?? std::placeholders::_1);
bf1(20); ///< same as Func(10?? 20)
class A
{
public:
int Func(int x?? int y);
};
A a;
auto bf2 = std::bind(&A::Func?? a?? std::placeholders::_1?? std::placeholders::_2);
bf2(10?? 20); ///< same as a.Func(10?? 20)
std::function< int(int)> bf3 = std::bind(&A::Func?? a?? std::placeholders::_1?? 100);
bf3(10); ///< same as a.Func(10?? 100)
??????????????У?bf1??????????????????????????????????10????????????μ???????????????????; bf2???????????????????????????????????????????????μ????????; bf3?????????????????????????????????????????μ?std::function???????????????????????????????????bind????????Щ????
??????1??bind???????????????????????????????????????????????pass-by-value??
??????2????????????????????????std::placeholders???????_1????????ε?????placeholder??pass-by-reference??
??????3??bind??????????????壬??????????std::function????
??????4???????????????????????????????????????????????????????Щ??????????
??????5?????this?????????????????????