You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无对应类型定义的谓词经std::function包装后可适配std::not2?

为什么自定义的new_same结构体没定义first_argument_type也能和std::not2一起用?

这背后的关键在于你用std::function<bool(int, int)>做了一层包装,而不是直接把new_same传给std::not2。咱们一步步拆解:

  • 首先,std::not2的设计确实要求传入的谓词类型必须具备first_argument_type和second_argument_type这两个类型定义——毕竟它要返回std::binary_negate对象,而binary_negate需要从谓词类型里提取这两个类型来继承std::binary_function。
  • 但你这里传入的不是new_same本身,而是被std::function<bool(int, int)>包装后的对象。std::function作为标准库的函数包装器,专门为符合二元函数签名的情况预设了对应的类型别名:当你实例化std::function<bool(int, int)>时,它会自动为你生成first_argument_type = int、second_argument_type = int以及result_type = bool,完美满足std::not2对谓词的类型要求。
  • 换句话说,std::function相当于给你的new_same补全了缺失的类型定义,让它适配了std::not2的接口要求。如果直接写auto not_same = std::not2(new_same());,编译器肯定会报错,但经过std::function的包装后,这个障碍就被消除了。

内容的提问来源于stack exchange,提问作者Oleg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 13:12:44