Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ template parameters to realize Length and TypeAt in Loki

Shulou Source: shulou.com Published: 2022-06-03 04:41:07 10月01日 Update

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

Tags: Templates parameters articles clips files more results support testing good practical complex code content readability situation bits articles extremes features Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Shulou Technology Redmi MySQL