在试图了解std::bind分配记忆的情况时,我看了一下这个答案,这给出了一些直觉,但我想要更详细的理解,所以我去了看源头gcc.
我检查下面的源代码用于std::bind从GCC实现的C++标准库.
/**
* @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)...);
}
Run Code Online (Sandbox Code Playgroud)
给定函数F和参数A和B,在哪里可以找到将它们复制到返回的数据结构中的代码,或者是否生成了此编译器?
这行:
__result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), std::forward<_BoundArgs>(__args)...);
Run Code Online (Sandbox Code Playgroud)
参数,可调用的(用 包装__do_wrap)和参数,都被转发到__result_type可能将它们存储在数据结构中的类型。
您应该查找代码__result_type,它将前者返回的数据包装在上面两行提到的实现定义类型中(即_Bind_helper<false, _Func, _BoundArgs...>::type)。
实际类型是_Bind(search class _Bind),它有一个接受可调用函数和参数的构造函数,当然,它是一个模板类(通过一些帮助程序公开)。
事实上,Bind_helper<whatever>::type(即返回的类型)被定义为typedef _Bind<whatever> type,您可以查找该类,仅此而已。
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |