惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
The Cloudflare Blog
博客园 - 聂微东
博客园 - 司徒正美
量子位
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
A
About on SuperTechFans

Jia Yue Hua

完美转发不完美 闲散颂 adapting c++20 ranges algorithms for most metaprogramming Reproducible github Developer Environments 使用mpmcpipeline和jthread实现软流水 在其它线程周期回调函数 在当前线程周期回调函数 for_each cpo和tag_invoke
transparent,为关联容器增加查找成员
Jia Yue Hua · 2023-05-18 · via Jia Yue Hua

folly::transparent 模板类可以用比较器实例化,从而支持异型查找:

#include <set>
struct S {
  template<class T, class U>
  bool operator()(T& a, U& b) const { return a < b; }
};
struct Man {
  int id_;
  auto operator<=>(const Man&) const = default;
};
struct ManComp {
  bool operator()(const Man &l,const int& r)const
  {
    return l.id_ < r;
  }
  bool operator()(const int& l, const Man& r)const
  {
    return l < r.id_;
  }
  bool operator()(const Man& l, const Man& r)const
  {
    return l.id_ < r.id_;
  }
};
void f() {
  std::set<Man,ManComp> m;
  m.find(11);//将报错
}
void g()
{
std::set<Man,folly::transparent<ManComp>> m;
  m.find(11);
}

上述f()中的m.find(11)将报错,因为find的参数只能接受Man类型,而int类型并不能隐式转化为Man,而g()将成功,因为比较器为folly::transparent一个实例,m.find()的查找重载成员将增加一成员模板函数,理论上模板函数的参数只要能和Key也就是Man比较就可。故而使用folly::transparent后成功.

Posted 2023-05-18