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

推荐订阅源

云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
L
LINUX DO - 最新话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
小众软件
小众软件
Jina AI
Jina AI
The Cloudflare Blog
P
Palo Alto Networks Blog
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
G
GRAHAM CLULEY
Project Zero
Project Zero
博客园_首页
博客园 - 三生石上(FineUI控件)
罗磊的独立博客
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
S
Securelist
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
IT之家
IT之家
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Last Watchdog
The Last Watchdog
T
Tenable Blog
宝玉的分享
宝玉的分享
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
量子位
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Security Archives - TechRepublic
Security Archives - TechRepublic

Dosk 技术站

华为荣耀电视盒子刷 Armbian - Dosk 技术站 记录一个没意义的 usb wifi 驱动编译 - Dosk 技术站 我的 2024 - Dosk 技术站 unRAID 7.0.0-beta.1 - Dosk 技术站 关于 unraider 的开心版 - Dosk 技术站 unRAID 6.12.7-rc2 进展 - Dosk 技术站 每年问自己 40 个问题 - Dosk 技术站 Kubernetes 集群导航页 - Dosk 技术站 2023 年度总结 - Dosk 技术站 N100 万兆软路由最终形态 - Dosk 技术站 N100 软路由改造下散热 - Dosk 技术站 N100 万兆软路由测测速 - Dosk 技术站 整活新玩具,半 DIY 一个万兆软路由 - Dosk 技术站 玩玩 WebAuthn:RP2040 One - Dosk 技术站 关于最近以及后续要做什么 - Dosk 技术站 unRAID 6.12.0-rc7/6.12.0-rc6.13 - Dosk 技术站 unRAID 6.12.0-beta18 试用 - Dosk 技术站 unRAID 6.11.4 如何继续开心 - Dosk 技术站 cert-manager 基于 acme.sh 的 webhook - Dosk 技术站 unRAID 虚拟机运行 unRAID 简单步骤 - Dosk 技术站 解决 Chrome 原生谷歌翻译功能的一个思路:3/3 - Dosk 技术站 解决 Chrome 原生谷歌翻译功能的一个思路:1/3 - Dosk 技术站 unraid 命令行包管理器 - Dosk 技术站 继续,unraid 6.11 happy 的方法 - Dosk 技术站 适用于 k8s 的 frpc ingress 实现 - Dosk 技术站 实现了一个简单的通配 DNS 服务 - Dosk 技术站 代码更新,happy unraid 懒人版 - Dosk 技术站 复盘一次群晖的 btrfs + docker:dind 引发的灾难 - Dosk 技术站 unraid 官方版不修改不用 keymaker 可持续 happy 的方法 - Dosk 技术站 unraid 实现完美硬盘启动 - Dosk 技术站 rpi3b+ 跑 pi-kvm 项目记录 - Dosk 技术站 记录一下 docker 里跑 dsm 迅雷套件的过程 - Dosk 技术站 docker 更新,整理 build - Dosk 技术站 记录下 openwrt 改交换机的步骤,以及注意事项 - Dosk 技术站 docker 里跑 openwrt 遇到的奇奇怪怪问题修复 - Dosk 技术站 OpenWrt 添加一个 socat 的 luci web 界面 - Dosk 技术站 Cloudflare DDNS 脚本,适配 OpenWrt,自带的不好用 - Dosk 技术站 在 NAS 里优雅的使用迅雷 - Dosk 技术站 记录一次 rpi4b 使用 openwrt docker 接管网络 - Dosk 技术站 悲伤是一件很耗费精力的事情 - Dosk 技术站 Tablet 板绘 - 梅花 - Dosk 技术站 Chromium 代码下并发 Callback 的骚操作 - Dosk 技术站 Tablet 板绘 - 月光 - Dosk 技术站 测试测试 Forestry - 一个 Jekyll CMS - Dosk 技术站 如何从 Chromium “偷” UI - Dosk 技术站 Cocoa 如何创建置于全屏之上的 NSWindow - Dosk 技术站 修改 Prose 支持中文标题 - Dosk 技术站 macOS & Windows 实现系统/进程/线程的 CPU 统计 - Dosk 技术站 Cocoa 修改 TitleBar 样式 - Dosk 技术站
STL 内 std::{bind/tuple/function} 简单实现 - Dosk 技术站
2020-02-19 · via Dosk 技术站

奇技淫巧还是运行函数时取相应的元组的对应位置的值,还是类似的方式,通过特化模板,公式是 <n, indexs...> => <n - 1, n - 1, indexs...>,比如 3 最后会生成 0 0 1 2 那么抛弃第一个,并用来展开元组,传递给函数指针

/*
 *  Author: SpringHack - springhack@live.cn
 *  Last modified: 2020-02-19 10:16:17
 *  Filename: main.cpp
 *  Description: Created by SpringHack using vim automatically.
 */
#include <iostream>

namespace dosk { // begin namespace dosk

// function
template <typename... T>
class function;

template <typename Result, typename... Args>
class function<Result(Args...)> {
  private:
    Result (*function_)(Args...);
  public:
    typedef Result return_type;
    function() = default;
    function(Result (*fn)(Args...)) : function_(fn) {};
    Result operator()(Args... a) {
      return function_(a...);
    }
    function& operator=(Result (*fn)(Args...)) {
      function_ = fn;
      return *this;
    }
};

// tuple
template <typename... T>
class tuple;

template <typename HEAD, typename... LIST>
class tuple<HEAD, LIST...> : public tuple<LIST...> {
  public:
    HEAD value;
    tuple(HEAD head, LIST... list) : tuple<LIST...>(list...), value(head) {};
};

template <>
class tuple<> {};

// tuple get
template <int index, typename... T>
class _tuple_type;

template <int index, typename HEAD, typename... LIST>
class _tuple_type<index, tuple<HEAD, LIST...>> {
  public:
    typedef typename _tuple_type<index - 1, tuple<LIST...>>::value_type value_type;
    typedef typename _tuple_type<index - 1, tuple<LIST...>>::tuple_type tuple_type;
};

template <typename HEAD, typename... LIST>
class _tuple_type<0, tuple<HEAD, LIST...>> {
  public:
    typedef HEAD value_type;
    typedef tuple<HEAD, LIST...> tuple_type;
};

template <int index, typename HEAD, typename... LIST>
typename _tuple_type<index, tuple<HEAD, LIST...>>::value_type get(tuple<HEAD, LIST...> t) {
  typedef typename _tuple_type<index, tuple<HEAD, LIST...>>::value_type value_type;
  typedef typename _tuple_type<index, tuple<HEAD, LIST...>>::tuple_type tuple_type;
  value_type rv = ((tuple_type)t).value;
  return rv;
}

// bind
template <size_t...>
class _tuple_index {};

template <size_t n, size_t... indexs>
class _make_indexs : public _make_indexs<n - 1, n - 1, indexs...> {};

template<size_t... indexs>
class _make_indexs<0, indexs...> {
  public:
    typedef _tuple_index<indexs...> index_type;
};

namespace placeholders {

template <size_t index>
class _placeholders {};

_placeholders<0> _1;
_placeholders<1> _2;
_placeholders<2> _3;
_placeholders<3> _4;
_placeholders<4> _5;
_placeholders<5> _6;
_placeholders<6> _7;
_placeholders<7> _8;
_placeholders<8> _9;
_placeholders<9> _10;

template <typename... RArgs>
class _unwrap_tuple {
  public:
    tuple<RArgs...> r_args; 
    _unwrap_tuple(tuple<RArgs...> r_args) : r_args(r_args) {};
    template <typename R>
    R operator[](R r) {
      return r;
    }
    template <size_t index>
    auto operator[](placeholders::_placeholders<index>) {
      return get<index>(r_args);
    }
};

};

template <typename Func, typename... Args>
class bind_t {
  public:
    typedef typename _make_indexs<sizeof...(Args)>::index_type _indexs;
    typedef typename Func::return_type return_type;
    Func func;
    tuple<Args...> args;
    bind_t(Func func, Args... args): func(func), args(args...) {}
    template <typename... RArgs>
    return_type operator()(RArgs&&... _r_args) {
      tuple<RArgs...> r_args = tuple<RArgs...>(_r_args...);
      return run(_indexs(), r_args);
    }
    template <size_t... Idx, typename... RArgs>
    return_type run(_tuple_index<Idx...>, tuple<RArgs...> r_args) {
      return func(unwrap_args<Idx>(r_args)...);
    }
    template <size_t index, typename... RArgs>
    auto unwrap_args(tuple<RArgs...> r_args) {
      placeholders::_unwrap_tuple<RArgs...> _u_a(r_args);
      auto _m_a = get<index>(args);
      return _u_a[_m_a];
    }
};

template <typename Func, typename... Args>
bind_t<Func, Args...> bind(Func& func, Args&&... args) {
  return bind_t<Func, Args...>(func, args...);
}

}; // end namespace dosk



// Test code
std::string test_func(int a, const char * b) {
  return std::to_string(a) + std::string(b);
}

std::string test_bind_args(int a, int b, int c, int d, int e) {
  return std::to_string(a) + std::to_string(b) + std::to_string(c) + std::to_string(d) + std::to_string(e);
}

int main() {
  // Test tuple
  dosk::tuple<int, const char *> t(123, "456");
  std::cout << dosk::get<0>(t) << dosk::get<1>(t) << std::endl;
  // Test function
  dosk::function<std::string(int, const char *)> closure_1 = test_func;
  std::cout << closure_1(123, "456") << std::endl;
  // Test bind
  dosk::function<std::string(int, int, int, int, int)> closure_2 = test_bind_args;
  auto binder = dosk::bind(closure_2, 1, dosk::placeholders::_2, 3, dosk::placeholders::_1, 5);
  std::cout << binder(4, 2, 0) << std::endl;
  return 0;
}