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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

Jia Yue Hua

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

daisy在cppnow 2023的演讲中展示了可极大简化模板元算法的技巧。 以排序为例 基本思路是 将type_list中的每个Type映射到他们的index, 然后对index数组做sort,结果为sorted_indices, 最终type_list中各个元素按照在sorted_indices新位置重新安放,返回结果type_list.

#include <type_traits>
#include <ranges>
#include <array>
#include <algorithm>
template <class...>
struct type_list {};

template <class List, auto Key>
struct Sort {};

template<size_t N, class ... Ts>
using at_t = std::tuple_element_t<N, std::tuple<Ts...>>;
template<class ... Ts, auto Key>
struct Sort<type_list<Ts...>, Key>
    : std::type_identity<decltype([]<size_t... Idx>(std::index_sequence<Idx...>) {
      constexpr auto sorted_indices = [] {
        std::array idxs = {Idx...};
        std::ranges::sort(idxs, [](size_t i, size_t j) {
          using variant_t = std::variant<std::type_identity<Ts>...>;
          std::array vars = {variant_t{std::in_place_index_t<Idx>{}}...  };
          return std::visit(Key, vars[i]) < std::visit(Key, vars[j]);
          });
        return idxs;
        
        }();
        return type_list<at_t<sorted_indices[Idx], Ts...>...>{};
    }(std::index_sequence_for<Ts...>{})

  )

> {};

int main(){

 using type=  Sort<type_list<double,int,char>, []<class T>(std::type_identity<T>){ return sizeof(T);}>::type ;
 static_assert(std::is_same_v<type, type_list<char,int,double> >);

}

这里的排序算法的comparator非常巧妙,

    [](size_t i, size_t j) {
            using variant_t = std::variant<std::type_identity<Ts>...>;
          std::array vars = {variant_t{std::in_place_index_t<Idx>{}}...  };
          return std::visit(Key, vars[i]) < std::visit(Key, vars[j]);
          }

我们需要取到type_list中第i个元素和第j个元素对应的信息,做法是构造一个variant的数组,variant的每项依次构造为std::type_identity<Ts>…,然后使用std::visit访问数组中的第i项和第j项,因为该lambda是constexpr的,所以可以用于编译时调用。

Key是以std::type_indentity为参数的仿函数

  []<class T>(std::type_identity<T>){ return sizeof(T);}

这里使用type_identity<T>而不是T做参数是因为,T可能是void或者数组或者不完全类型等不能作为参数的类型,而使用type_indentity<T>做参数可解决该问题。

main函数展示了如何使用该方法依据类型的size对typelist中的type排序。