In trying to understand the cirumstances under which std::bind
allocates memory, I looked at this answer, which gives some intuition, but I wanted a more detailed understanding, so I went and looked at the source for gcc
.
I am examining the following source code for std::bind
from the gcc implementation of the C++ standard library.
/**
* @brief Function template for std::bind.
* @ingroup binders
*/
template<typename _Func, typename... _BoundArgs>
inline typename
_Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
bind(_Func&& __f, _BoundArgs&&... __args)
{
typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
typedef typename __helper_type::__maybe_type __maybe_type;
typedef typename __helper_type::type __result_type;
return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
std::forward<_BoundArgs>(__args)...);
}
Given a function F and parameters A and B, where can I find the code that copies them into the returned data structure, or is this compiler generated?
This line:
__result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), std::forward<_BoundArgs>(__args)...);
Parameters, both the callable (wrapped with __do_wrap
) and the arguments, are forwarded to the __result_type
type that stores them likely in a data structure.
You should look for the code __result_type
, it wraps the data returned by the former in the implementation defined type mentioned two lines above (that is _Bind_helper<false, _Func, _BoundArgs...>::type
).
The actual type is _Bind
(search class _Bind
), which has a constructor that accepts both the callable and the arguments and, of course, is a template class (exposed by means of a few helpers around).
In fact, the Bind_helper<whatever>::type
(that is the returned type) is defined as typedef _Bind<whatever> type
, you can look for that class and that's all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With