实现一个简易版的c++11 tuple。

我使用的编译器是gcc,codeblocks13.12自带的,哪个版本我不熟gcc也没去查。

大致看了下他家的tuple实现,多继承,tuple之上还有2个辅助类,走的是类似loki中GenScatterHierarchy的路子。1092行代码,不是盖的。。。

有些强迫症,不打算用多继承,,虽然并不会实例化来,看着闹心。

只考虑实现到POD类型的基本支持就行了,什么右值之类的我还没看到,就不搞了,仅供参考。

个人觉得tuple保存POD类型值就足够了,泛滥就堕落了。

单继承版本确实比多继承版本美得多了,可变模板参数真是个好东西。

为了tuple_get只需使用static_cast,tuple是public继承的。当然私有继承更好,只是。。。我想tuple的使用应该没有地方会造成歧义吧。

提供了一个tuuple_get<int>(const tuple&)接口获取指定位置的值。

#ifndef HI_MPL_TUPLE_H_INCLUDE
#define HI_MPL_TUPLE_H_INCLUDE namespace hi {
using namespace mpl::utils;
//////////////////////////////////////////////////////////
template<typename... TList> struct tuple; template<> struct tuple<> {}; typedef tuple<> nulltuple; //////////////////////////////////////////////////////////
template<typename T, typename... TList>
struct tuple<T, TList...> : public tuple<TList...>
{
typedef T value_type;
typedef tuple<TList...> base_type;
typedef tuple<T, TList...> this_type; tuple(const T& v, const TList&... tails):base_type(tails...),_value(v) {} tuple(T&& v, TList&&... tails):base_type(std::move(tails)...), _value(std::forward<T>(v)) {}
tuple(T&& v, TList&... tails):base_type(std::move(tails)...), _value(std::forward<T>(v)) {}
tuple(T& v, TList&&... tails):base_type(std::move(tails)...), _value(std::forward<T>(v)) {} tuple(const this_type& other):base_type(static_cast<const base_type&>(other)),_value(other._value)
{} tuple(this_type&& other):base_type(std::move(static_cast<base_type&>(other))),_value(std::forward<T>(other._value))
{} const T& head() const { return this->_value; }
T& head() { return this->_value; } this_type& operator=(const this_type& other)
{
base_type::operator=(static_cast<const base_type&>(other));
_value = other._value;
return *this;
} this_type& operator=(this_type&& other)
{
base_type::operator=(std::move(static_cast<base_type&>(other)));
_value = other._value;
return *this;
} protected:
T _value;
}; template<typename T>
struct tuple<T> : public nulltuple
{
typedef T value_type;
typedef nulltuple base_type;
typedef tuple<T> this_type; tuple(const T& v):_value(v) {}
tuple(T&& v):_value(std::forward<T>(v)) {} tuple(const this_type& other):_value(other._value) {} tuple(this_type&& other):_value(std::forward<T>(other._value)) {} const T& head() const { return this->_value; }
T& head() { return this->_value; }
this_type& operator=(const this_type& other)
{
_value = other._value;
return *this;
}
this_type& operator=(this_type&& other)
{
_value = other._value;
return *this;
} protected:
T _value;
}; //////////////////////////////////////////////////////////
template<unsigned int N, typename... TList> struct tuple_at; template<unsigned int N, typename T, typename... TList>
struct tuple_at< N, tuple<T, TList...> >
{
typedef typename tuple_at< N-, tuple<TList...> >::value_type value_type;
typedef typename tuple_at< N-, tuple<TList...> >::tuple_type tuple_type;
}; template<typename T, typename... TList>
struct tuple_at< , tuple<T, TList...> >
{
typedef T value_type;
typedef tuple<T, TList...> tuple_type;
}; template<>
struct tuple_at<, nulltuple>
{
typedef nulltuple value_type;
typedef nulltuple tuple_type;
}; //////////////////////////////////////////////////////////
template<unsigned int N, typename... TList>
constexpr const typename tuple_at<N, tuple<TList...> >::value_type&
tuple_get(const tuple<TList...>& tuple_)
{
typedef tuple<TList...> tuple_type;
typedef typename tuple_at<N, tuple_type>::tuple_type base_tuple_type; return static_cast<const base_tuple_type&>(tuple_).head();
} template<unsigned int N, typename... TList>
typename tuple_at<N, tuple<TList...> >::value_type&
tuple_get(tuple<TList...>& tuple_)
{
typedef tuple<TList...> tuple_type;
typedef typename tuple_at<N, tuple_type>::tuple_type base_tuple_type; return static_cast<base_tuple_type&>(tuple_).head();
}
}
#endif

例子:

#include "TypeTuple.h"
#include <tuple> int main()
{
bool b;
tuple<int, float, char> pp = {, 0.1234, 'a'};
b = std::is_same<tuple_at<, tuple<int, float, char>>::value_type, char >::value;
std::cout << "is same: " << b << std::endl;
b = std::is_same<tuple_at<, tuple<int, float, char>>::tuple_type, tuple<char> >::value;
std::cout << "is same: " << b << std::endl;
std::cout << tuple_get<>(pp)<<" "<< tuple_get<>(pp) <<" "<<tuple_get<>(pp) << std::endl;
std::tuple<int, float, char> cc{, 0.1234, 'a'};
std::cout << sizeof(pp) << " " << sizeof(cc) << std::endl;
tuple<int, float, char> ppc = pp;
std::cout << tuple_get<>(ppc)<<" "<< tuple_get<>(ppc) <<" "<<tuple_get<>(ppc) << std::endl;
return ;
}

c++11 tuple实现的更多相关文章

  1. C++11 tuple元组

    C++11 tuple 元组 tuple容器(元组), 是表示元组容器, 是不包含任何结构的,快速而低质(粗制滥造, quick and dirty)的, 可以用于函数返回多个返回值; tuple容器 ...

  2. C++11 tuple

    tuple元组定义了一个有固定数目元素的容器,其中的每个元素类型都可以不相同,这与其他容器有着本质的区别.是对pair的泛化. 首先来介绍元组的创建和元组元素的访问.通过make_tuple()创建元 ...

  3. c++11——tuple元组

    tuple是一个固定大小的不同类型值的集合,是泛化的 std::pair.可以当做一通用的结构体使用,不需要创建结构体而又获取结构体的特征,在某些情况下可以取代结构体,使程序简洁.直观. 创建tupl ...

  4. c#编程指南(四) 组元(Tuple)

    (1).C#语法中一个个问号(?)的运算符是指:可以为 null 的类型. MSDN上面的解释: 在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型以及日期类型的功 ...

  5. C++11_ tuple

    版权声明:本文为博主原创文章,未经博主允许不得转载. tuple 是一个可以装载任何变量的容器,C++11的Variadic Templates给tuple的实现带来了极大方便. tuple的实现基于 ...

  6. 【python基础】第11回 数据类型内置方法 02

    本章内容概要 列表内置方法 字典内置方法 元组内置方法 集合内置方法 可变类型与不可变类型 本章内容详细 1.列表内置方法 list 列表在调用内置方法之后不会产生新的值 1.1 统计列表中的数据值的 ...

  7. Learning storm book 笔记8-Log Processing With Storm

    有代码的书籍看起来就是爽,看完顺便跑个demo,感觉很爽! 场景分析 主要是利用apache的访问日志来进行分析统计 如用户的IP来源,来自哪个国家或地区,用户使用的Os,浏览器等信息,以及像搜索的热 ...

  8. C++可变参数模板实现输出

    C++11 tuple&可变参数模板 template void Print(T value) { std::cout << value << std::endl; } ...

  9. 洗礼灵魂,修炼python(18)--温故加知新

    类型转换: 1.str(),repr(),format():将非字符串数据转换为字符串 str():对象序列化的结果,相当于print输出 repr():程序中某个对象精确值 format():利用特 ...

随机推荐

  1. redis pipline 和 事务

    1. Pipeline:“管道”,和很多设计模式中的“管道”具有同样的概念,pipleline的操作,将明确client与server端的交互,都是“单向的”:你可以将多个command,依次发给se ...

  2. MongoDB AUTH结果验证

      创建超级管理员和普通用户 #创建超级管理员 super db.createUser( { user: "super", pwd: "super", role ...

  3. AngelToken:区块链技术的突破

    科技进步,直接捅破了政治.金融.军事领域所有的玩法,让工业革命以来形成的规则变得一钱不值. 而且,当下的最重要的技术趋势——区块链.Token.AngelToken,正在引导我们走向全面的失控和未知. ...

  4. 封装一个函数,在ThinkPHP中一定程度上取代success和error

    1.下载jq2.下载layer3.根据自己的项目配置,将上述2文件放到样式目录中4.在Application/Common/function.php中添加如下代码function msg($msg,$ ...

  5. Linux c读取系统内存使用信息

    系统的内存使用信息能够在虚拟文件系统/proc/meminfo中找到,如图 所以只要打开/proc/meminfo文件,然后从中读取信息就好了 #include <stdio.h>#inc ...

  6. elastic search 重要的系统配置

    文章翻译自 https://www.elastic.co/guide/en/elasticsearch/reference/current/file-descriptors.html 1.文件描述符 ...

  7. [Leetcode 105]*前序后序遍历形成树

    public TreeNode find(int[] preorder, int[] inorder,int j, int start, int end) { if (j > preorder. ...

  8. lenet-5,Alexnet详解以及tensorflow代码实现

    http://blog.csdn.net/OliverkingLi/article/details/73849228

  9. ssh语法高亮

    借助于ssh,使用vi/vim进行文本编辑的语法高亮显示的方法如下: 第一步:设置vi别名 在Linux中,.bashrc与.bash_profile文件为当前用户登录时所执行的,/etc/bashr ...

  10. Python基础05_str_补充

    继续学习中, 补充一些str 的索引.切片.长度.循环输出等. #!/usr/bin/env python # coding:utf-8 # 通过下标索引获取字符串中的某个字符 # 1. 索引 tes ...