c++11 tuple实现
实现一个简易版的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实现的更多相关文章
- C++11 tuple元组
C++11 tuple 元组 tuple容器(元组), 是表示元组容器, 是不包含任何结构的,快速而低质(粗制滥造, quick and dirty)的, 可以用于函数返回多个返回值; tuple容器 ...
- C++11 tuple
tuple元组定义了一个有固定数目元素的容器,其中的每个元素类型都可以不相同,这与其他容器有着本质的区别.是对pair的泛化. 首先来介绍元组的创建和元组元素的访问.通过make_tuple()创建元 ...
- c++11——tuple元组
tuple是一个固定大小的不同类型值的集合,是泛化的 std::pair.可以当做一通用的结构体使用,不需要创建结构体而又获取结构体的特征,在某些情况下可以取代结构体,使程序简洁.直观. 创建tupl ...
- c#编程指南(四) 组元(Tuple)
(1).C#语法中一个个问号(?)的运算符是指:可以为 null 的类型. MSDN上面的解释: 在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型以及日期类型的功 ...
- C++11_ tuple
版权声明:本文为博主原创文章,未经博主允许不得转载. tuple 是一个可以装载任何变量的容器,C++11的Variadic Templates给tuple的实现带来了极大方便. tuple的实现基于 ...
- 【python基础】第11回 数据类型内置方法 02
本章内容概要 列表内置方法 字典内置方法 元组内置方法 集合内置方法 可变类型与不可变类型 本章内容详细 1.列表内置方法 list 列表在调用内置方法之后不会产生新的值 1.1 统计列表中的数据值的 ...
- Learning storm book 笔记8-Log Processing With Storm
有代码的书籍看起来就是爽,看完顺便跑个demo,感觉很爽! 场景分析 主要是利用apache的访问日志来进行分析统计 如用户的IP来源,来自哪个国家或地区,用户使用的Os,浏览器等信息,以及像搜索的热 ...
- C++可变参数模板实现输出
C++11 tuple&可变参数模板 template void Print(T value) { std::cout << value << std::endl; } ...
- 洗礼灵魂,修炼python(18)--温故加知新
类型转换: 1.str(),repr(),format():将非字符串数据转换为字符串 str():对象序列化的结果,相当于print输出 repr():程序中某个对象精确值 format():利用特 ...
随机推荐
- eclipse 运行 mapreduce程序报错 No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
报错信息 17/07/06 17:00:27 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Ap ...
- 『TensorFlow』读书笔记_降噪自编码器
『TensorFlow』降噪自编码器设计 之前学习过的代码,又敲了一遍,新的收获也还是有的,因为这次注释写的比较详尽,所以再次记录一下,具体的相关知识查阅之前写的文章即可(见上面链接). # Aut ...
- input date
https://stackoverflow.com/questions/17954966/how-to-get-rid-of-x-and-up-down-arrow-elements-of-a-inp ...
- 2015-10-06 认识jQuery1
jQue ...
- linux软件管理 YUM命令
yum的优点 将所有软件包放到官方服务器上,当进行yum在线安装时可以自动解决依赖性问题 yum源文件 [root@ssgao1987 yum.repos.d]# cd /etc/yum.repos. ...
- learning makefile automatic dependency generation
- Java基础-方法
方法 Java方法是语句的集合,它们在一起执行一个功能. 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 注意: 在一些其它语言中方法指过程和函数.一个 ...
- Putty查看生产环境下程序后台日志
双击桌面的putty.exe,然后选择session会话.登录用户.一般用户名为root. 登录成功之后在黑窗口敲以下命令行: cd /temp 到指定目录ls 查看该目录下文件tail -f n ...
- open live writer安装教程和账号配置
第一步:Open Live Writer软件下载.官方地址:http://openlivewriter.org/ 第二步:双击安装文件(OpenLiveWriterSetup.exe),然后点击下一步 ...
- JAVA学习笔记系列1-Java版本介绍
JavaSE(Java Standard Edition):标准版,定位在个人计算机上的应用(桌面应用).因为一般都是Windows系统,因此Java的这个发展并不好. JavaEE(Java Ent ...