As far as I understand,
std::bind perfectly conveys both the called object that it wraps and the arguments of this called object;- The returned object
std::binditself is movable and / or copyable, depending on whether the called object and its arguments are movable and / or copyable; - a The
std::bindreturned object may be nested, in which case the external returned object std::bindis movable and / or copyable, just like when binding other called objects.
Therefore, I expect the following code fragment to compile in order. Instead, the code generates a spew of compiler errors in the last two statements in main().
#include <functional>
template<typename HandlerType>
void call_handler(HandlerType&& handler)
{
handler();
}
template<typename HandlerType>
void do_something(HandlerType&& handler)
{
auto f = std::bind(
&call_handler<HandlerType&>,
std::forward<HandlerType>(handler));
f();
}
int main()
{
auto a = [&]() {};
do_something(a);
do_something(std::move(a));
auto b = std::bind([&]() {});
do_something(b);
do_something(std::move(b));
}
. , .
g++ 4.9.2 Cygwin f() do_something():
(4 of 103): error: no match for call to â(std::_Bind<void (*(std::_Bind<main()::<lambda()>()>))(std::_Bind<main()::<lambda()>()>&)>) ()â
Visual Studio 2013 :
1>C:\Program Files (x86)\Microsoft Visual Studio12.0\VC\include\functional(1149): error C2664: 'void (HandlerType)' : cannot convert argument 1 from 'void' to 'std::_Bind<false,void,main::<lambda_2b8ed726b4f655ffe5747e5b66152230>,> '
? std::bind?
,
- ?
- ,
std::bind , ? - ,
std::bind std::bind?
, .
EDIT: , , , std::ref - , , , f std::bind , a b std::bind call_handler f(), a b f, . , std::bind , , , , , .