用vs2002实现tupe/Cons,以及对应的make,at,size,for_each操作
实现 操作 for 以及 对应 at size Make
2023-09-27 14:21:05 时间
用vs2002实现tupe/Cons,以及对应的make,at,size,for_each操作
全特化,偏特化
https://harttle.land/2015/10/03/cpp-template.html
不支持偏特化的编译器的实现技巧
vs2002不支持偏特化,经过研究可以用struct内部包含的struct实现
比如,支持偏特化的编译器这样写
template <size_t N, class Cons>
struct At
{
};
而vs2002需要这样写
template <size_t N>
struct At
{
template <class Cons>
struct inner{};
};
全部代码如下
// testtuple.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
struct null_type{};
template <class T1, class T2 = null_type, class T3 = null_type, class T4 = null_type, class T5 = null_type>
struct tuple
{
T1 t1;
T2 t2;
T3 t3;
T4 t4;
T5 t5;
tuple(T1 t1_):t1(t1_)
{}
tuple(T1 t1_, T2 t2_):t1(t1_), t2(t2_)
{}
};
template <class T1, class T2 , class T3 , class T4 , class T5, class F>
void for_each(tuple<T1, T2, T3, T4, T5> &t, F f)
{
f(t.t1);
f(t.t2);
f(t.t3);
f(t.t4);
f(t.t5);
}
template <class Head= null_type, class Tail = null_type>
struct cons
{
typedef cons self_type;
typedef Head head_type;
typedef Tail tail_type;
Head head_;
Tail tail_;
cons(Head const &h, Tail const &t = Tail()):head_(h), tail_(t)
{}
};
template <class Head, class Tail>
cons<Head, Tail> make_cons(Head const &h, Tail const &t){
return cons<Head, Tail>(h, t);
}
template <class Head>
cons<Head> make_cons(Head const &h){
return cons<Head>(h);
}
template <size_t N>
struct At
{
template <class Cons>
struct inner
{
typedef typename At<N-1>::template inner<Cons::tail_type>::RET RET;
static RET &data(Cons &c) {
return At<N-1>::inner<Cons::tail_type>::data(c.tail_);
}
};
};
template <>
struct At<0>
{
template <class Cons>
struct inner
{
typedef typename Cons::head_type RET;
static RET &data(Cons &c) {
return c.head_;
}
};
};
template <class Cons>
struct Size
{
static const enum {value = 1 + Size<Cons::tail_type>::value };
};
template <>
struct Size <null_type>
{
static const enum {value = 0 };
};
template <>
struct Size <cons<> >
{
static const enum {value = 0 };
};
template <class Cons>
size_t size(Cons const &c)
{
return Size<Cons>::value;
}
template <class Cons>
struct ForEach
{
template <class F>
static void dof(Cons c, F f)
{
f(c.head_);
ForEach<Cons::tail_type>::dof(c.tail_, f);
}
};
template <>
struct ForEach<null_type>
{
template <class F>
static void dof(null_type c, F f)
{
}
};
template <>
struct ForEach<cons<> >
{
template <class F>
static void dof(cons<> c, F f)
{
}
};
template <class Cons, class F>
void for_each_cons(Cons c, F f)
{
ForEach<Cons>::dof(c, f);
}
struct Iter
{
void operator()(int v) const
{
std::cout << v << std::endl;
}
void operator()(null_type v) const
{}
};
int _tmain(int argc, _TCHAR* argv[])
{
tuple<int, int> t(1,2);
for_each(t, Iter());
std::cout << "----------------" << std::endl;
cons<int, cons<int> > c= make_cons(3, make_cons(2));
for_each_cons(c, Iter());
std::cout << "----------------" << std::endl;
At<1>::inner<cons<int, cons<int> > >::data(c) = 100;
size(c);
return 0;
}
相关文章
- 【需求响应】分时电价机制下居民用户需求侧对负荷响应研究(Matlab代码实现)
- SpringBoot操作MongoDB实现增删改查
- Amoeba软件实现mysql读写分离
- SpringMvc + Jsp+ 富文本 kindeditor 进行 图片ftp上传nginx服务器 实现
- C#实现顺序表定义、插入、删除、查找操作
- 转载 ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(三) 激动人心的时刻到啦,实现1v1聊天
- Spring IOC容器初始化后,执行一些逻辑操作的几种实现方式
- 基于Redis实现分布式锁以及任务队列
- Vue2.5 旅游项目实例20 详情页 实现Header渐隐渐显效果
- RedisTemplate实现scan操作
- Golang使用有缓冲channel实现协程池限制最大协程数量
- js实现一棵树的生长
- 双缓冲区绘图操作的实现
- 02-Linux Kernel(armv8-aarch64)的原子操作的底层实现
- Java实现单链表反转操作
- StringBoot+Vue实现游客或用户未登录系统前、可以浏览商品等信息、但是不能购买商品或者加入购物车等操作。登录系统显示用户的登录名(源码+讲解)
- Pytorch 学习(8):Recurrent layers (循环层)实现之GRUCell
- 用 Swift 枚举完美实现 3D touch 快捷操作
- PHP数组/Hash表的实现/操作、PHP变量内核实现、PHP常量内核实现 - [ PHP内核学习 ]
- 使用canvas实现擦除效果
- JavaScript中的单体模式四种实现方式
- C#--耗时操作实现UI界面实时更新不阻塞(耗时操作解决窗体卡顿)
- Java语言实现简单FTP软件------>连接管理模块的实现:主机与服务器之间的连接与关闭操作(八)