gpt4 book ai didi

c++ - gcc 的 std::bind 在哪里将参数复制到数据结构中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:59 25 4
gpt4 key购买 nike

为了了解 std::bind 分配内存的情况,我查看了 this answer ,这给出了一些直觉,但我想要更详细的了解,所以我去查看了 gcc 的源代码。

我正在检查 following source code对于来自 C++ 标准库的 gcc 实现的 std::bind

  /**
* @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)...);
}

给定一个函数 F 和参数 A 和 B,我在哪里可以找到将它们复制到返回的数据结构中的代码,或者这个编译器生成的?

最佳答案

这一行:

__result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)), std::forward<_BoundArgs>(__args)...);

可调用参数(用 __do_wrap 包装)和参数都被转发到 __result_type可能将它们存储在数据结构中的类型。

您应该寻找代码 __result_type ,它将前者返回的数据包装在上面两行提到的实现定义类型中(即 _Bind_helper<false, _Func, _BoundArgs...>::type )。
实际类型是_Bind (搜索 class _Bind ),它有一个接受可调用对象和参数的构造函数,当然,它是一个模板类(通过周围的一些助手公开)。
事实上,Bind_helper<whatever>::type (即返回类型)定义为 typedef _Bind<whatever> type ,您可以查找该类,仅此而已。

关于c++ - gcc 的 std::bind 在哪里将参数复制到数据结构中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36614574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com