C++ Metaprogramming::Typelist::Definitions and basic implementations

C++11 TypeList 妙用 源码展示: #include <iostream> using namespace std; template <typename> struct typelist; typedef typelist <int> defaultPolicys; template <typename> struct concat; template <typename> struct concat<typelist>, typelist<B> > { typ 立即下载

Version 1.00

Author: prototype

Copyright (C) 2004 prototype
Permission is granted to copy and redistribute this article provided that its contents (including the title, author information, and this copyright claim) are kept intact in the original form.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Abstract

The typelist and relevant concepts are introduced, together with a basic typelist implementation. An elegant, easy-to-use, and macro-free typelist generator is shown.


Introduction

Typelist, as its name suggests, refers to a (singly-) linked-list "data" structure with the data or elements being types. Like the conventional and more familiar singly-linked list data structure, typelist consists of a number of nodes that are arranged in a logically-linear fashion; specifically, each node conprises two fields: one, referred to as the "data field", holds the data (a type); the other one, referred to as the "pointer field", stores the information for finding the next node. The pointer field of the last node normally has a singular "value" indicating the end of the list. Typelist is as fundamental and important for C++ template metaprograming, which is becoming a crucial paradigm in moden C++ design and programming, as plain array for conventional programming. Examples of applications of typelist can be found in Andrei Alexandrescu's book: Modern C++ Design, and also in future installments of this C++ Metaprogramming series. Here I focus on a basic implementation of typelist and a solution to the interesting question: how to generate a typelist without C-type macros?

A basic implementation of typelist

Implementation of typelist can be amazingly simple as long as one knows how to store a data and how to represent a pointer in C++ template metaprogramming. The solutions to the two problems turn out to be the same -- use typedef. Let us take a look at an example to see how it works.

typedef int  type;    // Stores `int' to `type'.
type i;               // `i' is of type `int'.

This simple example illustrates that after the typedef, `type' is remembered by the compiler to stand for `int' and thereafter wherever `type' is used, it will work as if it was literally replaced by `int'. This effect is exactly what we want -- using a symbol to represent a data (a type). With this understood, storing a data in C++ template programming is straightforward; while the pointer used in typelist can be considered as an alias to the next node, therefore it can also be implemented using a typedef.

A basic implementation of typelist can be something like

template <typename T, typename SL>
struct typelist
{
   typedef T   type;
   typedef SL  sublist;
};

Obviously, with this implementation, we want to use `type' to store the data in this node and `sublist' to store the pointer to the next node. To construct a typelist would be something like

typedef typelist<int, typelist<bool, ?> >  tl2;

`tl2' here is a typelist with two nodes. In the first node, we store `int'; in the second node, `bool'. The `sublist' of the first node is `typelist<bool, ?>' -- the second node; but what about the `sublist' of the second node?

As I mentioned above we need a "singular" value for the pointer of the last node. So the `?' should be replaced by a type that is not used except for marking the end of a typelist. This type is easy to obtain, for example:

class null_type;

so the above `tl2' can be defined as:

typedef typelist<int, typelist<bool, null_type> >  tl2;

This works. But it turns out the best value is actually something like `typelist<null_type, null_type>'. This value is much more convenient to use as will illustrated in future installments. Here I would like to rationalize it a little bit through an analogy to the singular value for a pointer in normal programs.

It is known that 0 is used as the singular value for pointers in normal C++ programs. This value has two basic characteristics: first, it is a legal pointer value, which means it can be assigned to any pointer object; second, it semantically means “nowhere” in the memory. In analogy, what are needed for a singular value for the pointer in typelist are the following: first, it is a legal typelist value, which means that itself has to be a typelist; second, it semantically means there is no node after itself. Obviously, `typelist<null_type, null_type>' satisifies these two requirements much better than `null_type'.


A macro-free typelist generator

Generating a typelist as the example shown above is tedious and error-prone. Easier ways are definitely wanted. In some libraries, such as the famous LOKI, this task relies on the (evil) C macro with ugly interface. We want to do it in a much better way especially without all the hassle of C macros. More specifically, the challege is to implement the most clean and easy-to-use generator with an interface like:

typedef gen_typelist<T0, T1, T2, T3>::type  tl0;
// tl0 == typelist<T0, <typelist<T1, typelist<T2, typelist<T3, null_typelist> > > > >

typedef gen_typelist<T0, T1>::type  tl1;
// tl1 == typelist<T0, <typelist<T1, null_typelist> > >

This challenge comes with two problems to be solved: First, how to let the `gen_typelist' accept variable number of template arguments? Second, how to get the wanted typelist?

The C++ language supports default values for template parameters. Employing this, we can make a template to behave as if it can accept variable number of template arguments. For example,

template <typename T0, typename T1 = null_type, typename T2 = null_type>
struct gen_typelist
{
};

typedef gen_typelist<int>  t1;    // OK.
typedef gen_typelist<int, bool>  t2;    // OK.
typedef gen_typelist<int, bool, char>  v3;    // OK

This language feature can really be used to solve the first problem. Of course, there is an upper bound for the number of template arguments, but it is not a big problem if the template has a large number of parameters.
 
Now we get all the types for constructing the typelist in `gen_typelist'. The least thing we can do is to construct a typelist from the given types in a bruteforce way. To continue the above `gen_typelist' example, we get:

template <typename T0, typename T1 = null_type, typename T2 = null_type>
struct gen_typelist
{
    private:
        typedef typelist<T0, typelist<T1, typelist<T2, null_typelist> > >  crude_tl_;

    public:
};

This typelist is crude in the way that some `null_type' are possibly included. Next, we need to further refine it to remove all `null_type' except for the `null_typelist'. To do this, we process `crude_tl_' with a meta-program defined as follows:

template <typename T0, typename T1 = null_type, typename T2 = null_type>
struct gen_typelist
{
    private:
        typedef typelist<T0, typelist<T1, typelist<T2, null_typelist> > >  crude_tl_;

   template <typename T, typename L, int DUMMY = 0>
   struct refine1_
   {
      typedef typelist<T, L>  type;
   };

   template <int DUMMY>
   struct refine1_<null_type, null_typelist, DUMMY>
   {
      typedef null_typelist  type;
   };


   template <typename TL, int DUMMY = 0>
   struct refine_
   {
      typedef typename refine1_<typename TL::type, typename refine_<typename TL::sublist>::type>::type  type;
   };

   template <int DUMMY>
   struct refine_<typelist<null_type, null_typelist>, DUMMY>
   {
      typedef null_typelist  type;
   };
  
 public:
   typedef typename refine_<crude_tl_>::type  type;
};

typedef gen_typelist<int, int>::type  t2;    // t2 is `typelist<int, typelist<int> >'.

The magic is within the inner classes `refine_' and `refine1_'. How they acheive the functionality is an interesting problem left for the readers. :-)

Another way for refining the `crude_tl_' is using a recursive method, as follows:

template <typename T0, typename T1 = null_type, typename T2 = null_type>
struct gen_typelist
{
    typedef typelist<T0, gen_typelist<T1, T2> >  type;
};

template < >
struct gen_typelist<null_type, null_type, null_type>
{
    typedef null_typelist  type;
};

The specialization is for terminating the recursion.

Now, we have the facility for easy generation of typelist and can go ahead to have some fun in meta-programing. Before we go, some meta-functions for manipulating typelists are needed. I will show those in the next installment.

C++ 使用模板实现一个List的实例 主要介绍了 C++ 使用模板实现一个List的实例的相关资料,需要的朋友可以参考下 立即下载

相关推荐

空间节省算法在Rowhammer防御中的应用与优化

空间节省算法(Space-Saving)是一种高效的流式数据处理算法,主要用于在有限内存条件下实时识别数据流中的高频项。其核心原理是通过维护固定大小的计数器表,动态跟踪最活跃的元素,适用于处理无限数据流场景。该算法在内存安全领域具有重要价值,特别是在Rowhammer攻击防御中,能够有效检测异常高频访问的DRAM行地址。通过结合计数器衰减策略(如DSAC)和混合架构设计,空间节省算法可以在保证低硬件开销的同时,提供高精度的攻击检测能力。这些技术已成功应用于现代DDR5内存控制器,展现出优异的工程实践价值。

weixin_31139479的博客 606

(C++模板编程):typelist实现(类型计算的范例)

typelist实现(类型计算的范例) 【基本介绍】 《Modern C++ Design》 ---《C++设计新思维》-- Loki实现了typelist(列表头+嵌套列表技术来实现,因为2003年C++不支持可变参模板) typelist:用来操作一大堆类型的C++容器,就象C++标准库中的list容器(能够对各种数值提供各种基本操作),但typelist是针对类型进行操作。 typelist简单的说就是一个类型容器,能够对类型提供一系列的操作(把类型当成数据来操作)。 从实现上,typel

baidu_41388533的博客 2259

Zemax操作33--5倍激光扩束器

5倍扩束

lzy750750的博客 2932

Type List(C++ 模板元编程)

C++模板列表,提供一些编译期可操作函数

lifehappy 1000

coze开发-5、给智能体添加“技能”

中间编排与技能组件 中间编排的定义 中间编排是指在智能体的构建过程中,使用一些工具对智能体进行协调和管理。通过这些工具,我们能够更好地实现智能体在特定业务场景中的应用。

andyyah晓波的博客 527

工业数据采集实战:用open62541库读写PLC的OPC UA节点(避坑指南)

本文详细介绍了如何使用open62541库实现工业PLC的OPC UA节点数据采集,涵盖环境搭建、节点读写优化及生产环境故障排查。通过订阅模式和多节点读写技术,显著提升数据采集效率与稳定性,适用于智能制造和工业物联网场景。

weixin_30598225的博客 399

C++ type list 模板

【代码】C++ type list 模板。

站在树顶端设计的男人 561

画个板子玩一玩STM32F030F4P6,也许是最便宜的32bit MCU

前言,什么是STM32F030F4P6 STM32目前最便宜的芯片之一应该就是这一块,STM32F030F4P6,这一个芯片没有USB,但是其他都有,引脚少,价格低廉,如果找不到原厂,可以买替代的国产芯片,性能也一样。 不管怎么样也是Cortex-M0,比8bit和16bit的性能都强,总的来说就是低价,引脚少,体积小的同时,还容易开发,有很多模拟输入可玩,还有SPI,硬件I2C等等 所以,这一款MCU真的是从理论来说又便宜又好用,非常适合把玩。 画个板,最小系统 STM32F030其..

arenascat的博客 2万+

C++: TypeList(编译期的类型列表)

C++中的TypeList是一种编译期类型数据结构,用于模板元编程中的类型集合操作。摘要如下: 基本概念 TypeList是编译期类型容器,仅存储类型信息而不占用运行时内存 通过模板类实现,如template<typename...Ts> struct TypeList{}; 核心操作 访问:获取类型列表长度、索引特定位置类型 查询:检查类型包含、查找类型索引 修改:追加/删除类型、类型映射转换 组合:连接类型列表、分割列表 典型应用 生成访问者模式实现 工厂模式类型管理 序列化框架开发 类型安

红军不怕远征难,万水千山只等闲 1450

一个中科大差生的8年程序员工作总结

今年终于从大菊花厂离职了,离职前收入大概60w不到吧,在某乎属于比较差的,今天终于有空写一下自己的职场故事,也算是给自己近8年的程序员工作做个总结复盘。近8年有些事情做对了,也有更多事情做错了,在这里记录一下,希望能够给后人一些帮助吧,也欢迎私信交流。文笔不好,见谅,有些细节记不清了,如果有出入,就当是我编的这个故事吧。 PS:有几个问题先在这里解释一下,评论就不一一回复了 1、关于差生,我本人在科大时确实成绩偏下,差生主要讲这一点,没其他意思。 2、因为买房是我人生中的大事,我认为需要记录和总结一下

陈小房的自我修炼 5万+

游戏开发:逻辑与渲染分离技术解析

游戏开发中逻辑与渲染分离的架构设计 本文系统阐述了游戏开发中逻辑与渲染分离的设计原则与实践方法。文章首先定义了核心概念:逻辑层负责游戏规则与状态,渲染层处理视觉表现,二者通过数据模型层进行通信。通过历史回顾,揭示了从早期混合架构到现代分层设计的演变过程。 在技术实现层面,文章详细分析了分离架构的优势:性能优化、流畅度控制、测试便利性、扩展灵活性以及团队协作效率。提供了Unity和Unreal两大引擎的具体实现方案,包括代码示例、分层模型和最佳实践。特别探讨了网络同步、客户端预测、插值处理等关键技术难点。 文

本博客聚焦游戏开发技巧、创业实战心得及大学热门课程干货。这里既有技术深度,也有思维广度,无论你是开发者、创业者还是高校学子,都能在这里找到适合自己的成长之路! 1060

【元编程】typelist的实现

typelisttypelisttypelist这个概念比较系统的出现是在ModernCDesignModernCDesign一书中,关于typelisttypelisttypelist的解释,就是用来操作一大堆类型的CC++C容器,就像std::list一样能够为数值提供各自操作一样,不过这里操作的是类型,而不是数值。这里,基于C11C++11C11以上的标准实现typ。

Antonio915的博客 1227

Qt实现窗口自适应分辨率大小

如果希望窗口大小始终保持与屏幕分辨率一致,可以将上述代码放在窗口的resizeEvent()函数中。这样,每当窗口大小发生变化时,都会自动调用resizeEvent()函数,从而实现窗口大小的自适应调整。在Qt中,可以通过一些简单的方法实现窗口大小随分辨率变化而自适应调整。获取到当前屏幕的分辨率后,可以根据需要调整窗口的大小。无论在何种分辨率下运行应用程序,窗口都能够正确地自动调整大小,提供更好的用户体验。除了调整窗口大小,还可以考虑将窗口设置为最大化,以充分利用屏幕空间。步骤2:调整窗口大小。

FuJavascript的博客 3655

type_list在现代C++中的妙用,90%的资深工程师都忽略的关键细节

掌握C++模板元编程的type_list,轻松实现编译期类型安全与高效抽象。适用于泛型库设计、反射系统构建等场景,通过递归展开与模式匹配提升性能。揭示90%工程师忽略的关键细节,值得收藏。

InitFlow的博客 423

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

type_list完全指南,彻底搞懂C++编译期类型列表的底层机制

掌握C++模板元编程的type_list,彻底理解编译期类型列表的实现机制。涵盖类型查询、转换与算法操作,适用于泛型库设计与编译期优化。核心方法清晰解析,提升代码效率与灵活性,值得收藏。

PixelGlow的博客 1102

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

C++模板编程——typelist的实现

本文主要介绍了使用元编程编写typelist,并给出了具体的可运行的代码。

T_killer_的博客 704

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

跟我学c++高级篇——模板元编程之七typelist

对元编程中的typelist进行分析说明

fpcc的专栏 931

C++: TypeList

#include &lt;stdio.h&gt;#include &lt;typeinfo.h&gt;//// TypeList//template &lt;class T, class U&gt;struct TypeList{    typedef T Head;    typedef U Tail;};struct NullType{};#define TYPELIST_1(T1)     ...

weixin_34253126的博客 223

【C++泛型编程】Typelists型别工具

Typelists是一个用来操作一大群型别的C++工具,就像lists对数值提供各种基本操作一样。 1.定义Typelists            Typelist是以一种特殊的型别结束:NullType class NullType {}; struct EmptyType {}; // could be instantiated template st

xiaoding133的专栏【Stay hungry,Stay foolish】 1537

c++ 泛型编程 之 TypeLists

完整代码 在  http://download.youkuaiyun.com/detail/zhuyingqingfen/8457091关于 C++ 泛型中的 TypeTraits ,参考 c++ 泛型编程 之 TypeTraits#ifndef TYPE_LISTS_H_ #define TYPE_LISTS_H_ #include #include #include "typetraits.h"

UnrealEngine4 、Delta3D、OSG 2689

c++模版学习(typelist)2021-03-05

namespace tplt { //基本的操作接口 template <typename ... T> class typelist { }; template <typename TPLT> class front; template <typename fisrt, typename ...other> class front<typelist<fisrt, other...

网瘾少年丶的博客 286

C++ 模板特化以及Typelist的相关理解

  近日,在学习的过程中第一次接触到了Typelist的相关内容,比如Loki库有一本Modern C++ design的一本书,大概JD搜了一波没有译本,英文版600多R,瞬间从价值上看到了这本书的价值!!这是题外话。这本书十分经典。其内容对于一个C++新手来说需要时间来理解吸收。在这里记录一下自己的理解。日后发现错误会给予更正。如有有人碰巧看到了。欢迎指正。 参考了http://blog.c...

weixin_30613727的博客 222

将任意类型映射到一个唯一整数(C++模板实现TypeList)

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34273479的博客 417

modern c++ design 笔记(如何列印一个TYPELIST)

class NullType{};template void PrintTList(TList &dummy){coutPrintTList(TList::Tail);} void PrintTList(NullType &dummy){cout}

程序亦道---das_jack的专栏 545

C++ 的MetaProgramming 入门篇

C++ 的另一个新世界C++ 的 MetaProgramming 废话就不说了, 按照C的传统惯例,介绍programming的最好方式就是show代码, 第一个例子就是Hello,world, 这篇文章也不例外在任何一个cpp文件中,输入struct hello_world; //forward declarationstruct A : hello_world{};然后编译..,注意我没有说"

ilovevc的专栏 3178
下一篇: My "C++ Metaprogramming::..." is infinitely postponed.
prototype
博客等级 码龄25年 9粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值