How to use C++ template parameters to realize Length and TypeAt in Loki
This article will explain in detail how to use C++ template parameters to achieve Length and TypeAt in Loki, the editor thinks it is very practical, so share it with you for reference. I hope you can get something after reading this article.
First, the simulation implementation of Length and TypeAt in the original Loki is as follows
1, template file
# ifndef _ SZYU_LOKI__#define _ SZYU_LOKI__#include # include template class TypeList {public: typedef T Head; typedef U Tail;}; class NullType # define TYPELIST_1 (type1) TypeList#define TYPELIST_2 (type1, type2) TypeList#define TYPELIST_3 (type1, type2) Type3) TypeList/* Loki Length***/template class Length Template class Length {public: enum {value = 1 + Length::value};}; template class Length {public: enum {value = 0};} / * Loki TypeAt***/template class TypeAt;template class TypeAt {public: typedef typename TypeAt::Result Result;}; template class TypeAt {public: typedef T Result;} # endif
2, test cases
/ * Author: szyu** Date: 17 / 7 / # include "loki.h" voidtest1 () {std::cout