实现一个简易版的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. Ajax+Struts2用户注册功能实现

    详细请参考源码(Github):https://github.com/QQ3330447288/ajaxRegister 1.目录结构 2.截图 3.核心代码: register.jsp <sc ...

  2. 美国FLAG和中国BAT的比较(王益)

    美国FLAG和中国BAT的比较(王益) http://cxwangyi.github.io/notes/2014-09-29-flag-vs-bat.html 知乎 http://www.zhihu. ...

  3. 『TensorFlow』读书笔记_Inception_V3_下

    极为庞大的网络结构,不过下一节的ResNet也不小 线性的组成,结构大体如下: 常规卷积部分->Inception模块组1->Inception模块组2->Inception模块组3 ...

  4. 团队作业8——敏捷冲刺(Beta阶段)

    Beta阶段--第1篇 Scrum 冲刺博客(计划) https://www.cnblogs.com/just-let-it-go/p/9061664.html Beta阶段--第2篇 Scrum 冲 ...

  5. Module(模块)

    1.每个Angular至少有一个根Module 2.Module时一个带有@NgModule装饰符的类 3.最简单的Module import { NgModule } from '@angular/ ...

  6. 深入剖析Java中的装箱和拆箱(缓存池技术)

    以下是本文的目录大纲: 一.什么是装箱?什么是拆箱? 简单一点说,装箱就是  自动将基本数据类型转换为包装器类型:拆箱就是  自动将包装器类型转换为基本数据类型. 二.装箱和拆箱是如何实现的 1:反编 ...

  7. leetcode python 010

    #实现正则表达式匹配并支持'.'和'*'.#''匹配任何单个字符.#'*'匹配前面元素的零个或多个.#匹配应覆盖整个输入字符串(非部分).##Some examples:##isMatch(" ...

  8. 复制目录及其子目录下所有文件DOC

    echo 开始同步代码 set src_home=F:\work\sign\Bonade-Sign set dest_home=F:\work\testGit\sign5\Bonade-Sign se ...

  9. Codeforces Round #324 (Div. 2) (哥德巴赫猜想)

    题目:http://codeforces.com/problemset/problem/584/D 思路: 关于偶数的哥德巴赫猜想:任一大于2的偶数都可写成两个素数之和. 关于奇数的哥德巴赫猜想:任一 ...

  10. Linux系统基本操作命令

    1.文件与文件夹(目录)操作命令 可以参考:http://www.runoob.com/linux/linux-command-manual.html 1)rm:删除命令 例:$  rm  -i  文 ...