tuple

Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.

可以用来在bind中存储函数指针和参数

实现思路

不定参数模板 + 模板偏特化

tuple作为一个存储不定参数模板的类,分为一下几种偏特化形式:

template<>

struct tuple<> {};

template<typename T, typename... Args>

struct tuple<T, Args...> : public tuple<Args...>

template

struct tuple : public nulltuple

可以注意到每个模板struct tuple<T, Args...> : public tuple<Args...>作为其上一级模板的子类来展开,这样在获取tuple_at时从前向后维护一个当前元素的下标计数来存取元素

源码

template<typename... Args> struct tuple;

	template<>
struct tuple<> {};
typedef tuple<> nulltuple; template<typename T, typename... Args>
struct tuple<T, Args...> : public tuple<Args...>
{
typedef tuple<T, Args...> self_type;
typedef tuple<Args...> base_type;
typedef T value_type;
tuple(const T& v, const Args&... tails) :base_type(tails...), value(v) {}
tuple(T&& v, Args&&... tails) :base_type(std::move(tails)...), value(std::forward<T>(v)) {}
tuple(T&& v, Args&... tails) :base_type(std::move(tails)...), value(std::forward<T>(v)) {}
tuple(T& v, Args&&... tails) :base_type(std::move(tails)...), value(std::forward<T>(v)) {} T& getval()
{
return value;
} T value;
}; template<typename T>
struct tuple<T> : public nulltuple
{
typedef T value_type;
tuple(const T& val) :value(val) {} T& getval()
{
return value;
} T value;
};
template<size_t n, typename T> struct tuple_at; template<size_t n, typename T,typename... Args>
struct tuple_at<n, tuple<T,Args...>>
{
typedef typename tuple_at<n - 1, tuple<Args...>>::value_type value_type;
typedef typename tuple_at<n - 1, tuple<Args...>>::tuple_type tuple_type;
}; template<typename T,typename... Args>
struct tuple_at<0, tuple<T,Args...>>
{
typedef T value_type;
typedef typename tuple<T, Args...> tuple_type;
}; template<typename T>
struct tuple_at<0, tuple<T>>
{
typedef T value_type;
typedef typename tuple<T> tuple_type;
}; template<>
struct tuple_at<0,tuple<>>
{
typedef tuple<> tuple_type;
typedef tuple<> value_type;
}; template<size_t n,typename... Args>
typename tuple_at<n, tuple<Args...>>::value_type& tuple_get(tuple<Args...>& t)
{
typedef typename tuple_at<n, tuple<Args...>>::tuple_type ret_tuple_type;
return static_cast<ret_tuple_type&>(t).getval();
}

从零开始写STL—模板元编程之tuple的更多相关文章

  1. 从零开始写STL—模板元编程之any

    any class any; (since C++17) The class any describes a type-safe container for single values of any ...

  2. 从零开始写STL - 智能指针

    从零开始写STL - 智能指针 智能指针的分类及其特点: scoped_ptr:初始化获得资源控制权,在作用域结束释放资源 shared_ptr: 引用计数来控制共享资源,最后一个资源的引用被释放的时 ...

  3. javascript 元编程之 method_missing

    javascript 元编程之 method_missing 引言 要说元编程 ruby 中技巧太多了,今天来写的这个技术也来自于 ruby 中的灵感. method_missing 这个在 ruby ...

  4. 从零开始写STL—栈和队列

    从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...

  5. 从零开始写STL—functional

    function C++11 将任意类型的可调用(Callable)对象与函数调用的特征封装到一起. 这里的类是对函数策略的封装,将函数的性质抽象成组件,便于和algorithm库配合使用 基本运算符 ...

  6. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  7. 技术解析丨C++元编程之Parser Combinator

    摘要:借助C++的constexpr能力,可以轻而易举的构造Parser Combinator,对用户定义的字符串(User defined literal)释放了巨大的潜力. ## 引子 前不久在C ...

  8. Javascript元编程之Annotation

    语言的自由度 自由度这个概念在不同领域有不同的定义,我们借鉴数学中构成一个空间的维数来表达其自由度的做法,在此指的是:解决同一个问题彼此不相关的设计方法学数量. 例如,解决一个比如商品打折的问题,如何 ...

  9. 从零开始写STL—哈希表

    static const int _stl_num_primes = 28; template<typename T, typename Hash = xhash<T>> cl ...

随机推荐

  1. Hadoop分布式集群安装

        环境准备     操作系统使用ubuntu-16.04.2 64位 JDK使用jdk1.8 Hadoop使用Hadoop 2.8版本     镜像下载  操作系统     操作系统使用ubun ...

  2. [Python學習筆記] 利用 Python在Excel 插入註解

    用Python 來處理excel 檔 用過了 openpyxl 還有 pyexcel目前覺得除了讀寫如果還要使用另外的功能 (像是讀取格子裡的公式)可以用 xlwings  他的首頁標題 " ...

  3. swift的static和class修饰符---What is the difference between static func and class func in Swift?

    Special Kinds of Methods Methods associated with a type rather than an instance of a type must be ma ...

  4. 汇编2.汇编版本的helloworld

    寻址方式 立即数寻址 寄存器寻址 存储器寻址 直接寻址 : mov ax, [ 01000h ]; 直接在[]内给出一个内存地址 寄存器间接寻址: mov ax ,[si]; 在[]以寄存器的值给出内 ...

  5. vuez init webStorm

    <!-- * @description text !--> <template> <div>#[[$END$]]#</div> </templat ...

  6. 油猴 tamperMonkey 在百度首页 添加自己的自定义链接

    发现 GM_addStyle 函数不能用了,从写加载css函数. 剩下找个定位 添加内容 就很简单了. // ==UserScript== // @name helloWorld // @namesp ...

  7. cocos2dx 接入bugly 报错 Fail to get class by NSClassFromString(BuglyAgent)

    ios 端安装文档接入库后,报错 -> static void BuglyJSAgent::reportJSError(JSContext *, const char *, JSErrorRep ...

  8. ideal取消按下两次shift弹出搜索框 修改idea,webstrom,phpstrom 快捷键double shift 弹出search everywhere

    因为经常需要在中英文之间切换,所以时常使用shift键,一不小心就把这个Searchwhere 对话框调出来了,很是麻烦. 因此痛定思痛, 我决定将这个按两下shift键就弹出搜索框的快捷键禁用了! ...

  9. Mac 下用homebrew安装配置MongoDB

    ---恢复内容开始--- 1.首先安装homebrew,已有就跳过 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent. ...

  10. idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件

    1.集成到开发环境中 以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可.(注意此处是以plugin的方式,放在<plugins></plugins>中间 ...