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的实例的相关资料,需要的朋友可以参考下 立即下载

相关推荐

【导航】ESP32 教学篇目录 【快速跳转】

本文章 来自原创专栏《ESP32教学专栏 (基于ESP-IDF)》,讲解如何使用 ESP-IDF 构建 ESP32 的WIFI及连接的程序,发布文章并会持续为已发布文章添加新内容! 每篇文章都经过了精打细磨! ↓↓↓通过下方 按钮 进入专栏主页↓↓↓ 专栏主页:点击进入→ESP32 教学专栏 (基于ESP-IDF) WIFI二级专栏:点击进入→ESP32 上的 WiFi ............

Augtons 的博客 3万+

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

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

baidu_41388533的博客 2264

雷达原理习题答案【西电】

  目前教材用的是陈老师新版的,但是,我发现课后题有一部分是之前某本书里的。答案如下【建议参看原题→查阅答案】 【原题点这里!!点这里!!】 目录跳转功能目前好像只支持网页版 文章目录% 第一章、第二章% 第三章% 第四章% 第五章% 第六章% 第七章% 第八章 % 第一章、第二章 % 第三章 % 第四章 % 第五章 % 第六章 % 第七章 % 第八章 ...

薛定谔的壳的博客 1万+

C++ type list 模板

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

站在树顶端设计的男人 562

[SDIO] SD card 初始化及常用命令解析(附波形,uboot代码)

目录 初始化 1.cmd 0 2.cmd8 3.cmd55 4.acmd41 5.cmd2 6.cmd3 7.cmd9 8.cmd13 9.cmd7 10.ACMD51 11.CMD6 12.CMD16 13.CMD17 14.CMD18 15.CMD12 tuning CMD19 DW_SDHCI的tuning流程 初始化 1.cmd 0 /* Reset the Card */ err = mmc_go_idle(mmc...

Lenz_s_law的开发记录本 2万+

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

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

lifehappy 1002

史上最小白之RNN详解

1.前言 网上目前已经有诸多优秀的RNN相关博客,但是我写博客的出发点主要是为了加深和巩固自己的理解,所以还是决定自己再进行一下总结和描述,如有不正确的地方欢迎指正~ 2.区分RNN 循环神经网络(Recurrent Neural Network),递归神经网络(Recursive Neural Network),你有没有发现他们的缩写都是RNN,那他们两个是同一回事儿吗?网上有一些博客把这两就当...

Tink1995的博客 4万+

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

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

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

华为OD机试真题目录汇总(Java语言)2024 B+C+D+E卷 (560道)

本文是2023华为OD机试真题(Java 语言) B卷专栏的目录贴(定期更新2023华为OD各个时间阶段的机试真题,每日定时更新,本专栏将使用Java语言进行更新解答,包含真题,思路分析,代码参考,每个题目的思路分析都非常详细,超过百字欢迎大家订阅学习,代码可以直接运行使用,全网限时最低价,持续更新)华为OD是Outsourcing Dispacth模式,是华为和外企德科联合招聘的简称。目前华为大多数是OD招聘。OD模式也是华为提出的一种新的用工形式,每年都会从OD项目挑优秀员工转为正编。

KJ.JK 8万+

异常数据(outlier data)处理基于分位数法(箱图法、箱型法)替换及删除方案

异常数据(outlier data)处理基于分位数法(箱图法、箱型法)替换及删除方案 简单统计法: 这个原则有个条件:数据需要服从正态分布。 在3倍标准差原则下,异常值如超过3倍标准差,那么可以将其视为异常值。正负3的概率是99.7%,那么距离平均值3之外的值出现的概率为P(|x-u| > 3) <= 0.003,属于极个别的小概率事件。如果数据不服从正态分布,也可以用远离平均值的多少倍标准差来描述。 分位数法(箱图法、箱型法): 并不是说从高斯分布中抽出的所有数据都符..

data+scenario+science+insight 1806

【元编程】typelist的实现

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

Antonio915的博客 1235

电磁弹射器电源设计参数

电磁弹射器采用中高压直流供电系统(5,000-12,000V DC),以满足航母弹射的高功率需求(单次120+兆焦耳)。典型系统如福特级航母EMALS,峰值功率达100-130MW,工作电压8-10kV DC,通过高压设计降低电流(15-20kA)并提升效率20%。系统包含整流、储能和脉冲逆变环节,电压随载荷动态调整(轻型4-6kV,重型8-12kV)。采用特殊绝缘材料和模块化设计确保安全,相比蒸汽弹射节能30%且控制更精准(误差<±2%)。

336

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

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

T_killer_的博客 707

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

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

PixelGlow的博客 1107

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

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

InitFlow的博客 424

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

跟我学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的博客 224

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

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

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

c++ 泛型编程 之 TypeLists

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

UnrealEngine4 、Delta3D、OSG 2693

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的用法

在Modern C++ Design中提到了很多关于模板的用法,给我印象最深的是Typelist,其他的主要是看不懂,所以不知道有什么用,所以这里不谈。 但是对于Typelist,书中讲述了如何实现,但却没有用法介绍,我这里给出一个例子: #include #include #include "typelist.h" using namespace std; struct MsgRe

blade_ly的专栏 1295

Modern C++ Design 笔记 第三章 3. TypeList

 套用一个现在很流行的句式,(好多什么编程之美或者代码之美的)。我想看了第三章Modern C++ Design能感受的就是递归之美。而其中主要介绍的TypeList就是这样一个递归演绎的完美的例子。其实TypeList本身的定义非常简单。就是一个有Head有Tail的一个简单的空的定义template struct Typelist { typedef T Head; typ

斯巴达的勇士已经是雅典的臣民了。。。 2607

C++ 的MetaProgramming 入门篇

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

ilovevc的专栏 3180

原型模式(Prototype)C++实现

 原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节下面是其UML模型和代码实现: //prototype.h#ifndef AFX_CLASS_PROTOTYPE#define AFX_CLASS_PROTOTYPEclass Prototype{pub

编程菜鸟---正在努力进阶 2618
下一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址