如何使用C++模板变参实现Loki中的Length和TypeAt

这篇文章将为大家详细讲解有关如何使用C++模板变参实现Loki中的Length和TypeAt,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一,原Loki中的Length和TypeAt模拟实现如下

1,模板文件

#ifndef __SZYU_LOKI__
#define __SZYU_LOKI__

#include <iostream>
#include <vector>

template <typename T, typename U>
class TypeList
{
public:
    typedef T Head;
    typedef U Tail;
};

class NullType;

#define TYPELIST_1(type1)               TypeList<type1, NullType>
#define TYPELIST_2(type1, type2)        TypeList<type1, TYPELIST_1(type2)>
#define TYPELIST_3(type1, type2, type3) TypeList<type1, TYPELIST_2(type2, type3)>

/*******************************************
*
* Loki Length
*
**********************************************/
template <typename T> class Length;

template <typename T, typename U>
class Length<TypeList<T, U> >
{
public:
    enum { value = 1 + Length<U>::value };
};

template <>
class Length<NullType>
{
public:
    enum { value = 0 };
};

/*******************************************
*
* Loki TypeAt
*
**********************************************/
template <typename T, unsigned int index> class TypeAt;

template <typename T, typename U, unsigned int index>
class TypeAt<TypeList<T, U>, index>
{
public:
    typedef typename TypeAt<U, index - 1>::Result Result;
};

template <typename T, typename U>
class TypeAt<TypeList<T, U>, 0>
{
public:
    typedef T Result;
};

#endif

2,测试用例

/**********************************                                                              
*
* Author : szyu
*
* Date : 2017.1.7
*
**************************************/

#include "loki.h"

void
test1()
{
    std::cout << "Length<TYPELIST_1(int)>..." << Length<TYPELIST_1(int)>::value << std::endl;
    std::cout << "Length<TYPELIST_2(int, int)>..." << Length<TYPELIST_2(int, int)>::value << std::endl;
    std::cout << "Length<TYPELIST_3(int, int, int)>..." << Length<TYPELIST_3(int, int, int)>::value << std::endl;

    std::cout << "************************************" << std::endl;
    std::vector<int> IntVector;

    std::cout << typeid(TypeAt<TYPELIST_3( double, std::string, std::vector<int>), 0>::Result).name() << std::endl;
    std::cout << typeid(TypeAt<TYPELIST_3( double, std::string, std::vector<int>), 1>::Result).name() << std::endl;
    std::cout << typeid(TypeAt<TYPELIST_3( double, std::string, std::vector<int>), 2>::Result).name() << std::endl;
    
    TypeAt<TYPELIST_3( double, std::string, std::vector<int>), 0>::Result var1;
    var1 = 10.10;
    
    TypeAt<TYPELIST_3( double, std::string, std::vector<int>), 1>::Result var2;
    var2 = "abc";

    TypeAt<TYPELIST_3( double, std::string, std::vector<int>), 2>::Result var3;
    var3 = IntVector;
}

int
main( int argc, char *argv[] )
{
    test1();

    return 0;
}

3,执行结果如下:

如何使用C++模板变参实现Loki中的Length和TypeAt

二,使用C++11模板变参特性改进Length和TypeAt

        在不使用模板变参会出现繁杂的TypeList嵌套问题,虽然使用宏定义能增加代码可读性,但是也因此需要定义大量的宏来支持。当出现几百个参数(极端)情况下,那需要定义几百个宏。显然比较麻烦。C++11提供了模板的变参的支持,故变参实现Length和TypeAt如下:

1,头文件

/**********************************                                                              
*
* Author : szyu
*
* Date : 2017.1.6
*
***********************************/

#ifndef __SZYU_LOKI__
#define __SZYU_LOKI__

#include <iostream>
#include <vector>

/***********************************
*
* Loki length
*
***************************************/
template <typename T, typename... Args>
class Length
{
public:
    enum { value = 1 + Length<Args...>::value };
};

template <typename T>
class Length<T>
{
public:
    enum { value = 1 };
};

/***********************************
*
* Loki TypeAt
*
***************************************/
template <unsigned int index, typename T, typename... Args>
class TypeAt
{
public:
    typedef typename TypeAt<index - 1, Args...>::Result Result;
};

template <typename T, typename... Args>
class TypeAt<1, T, Args...>
{
public:
    typedef T Result;
};

#endif

2,测试用例

/**********************************                                                              
*
* Author : szyu
*
* Date : 2017.1.6
*
***********************************/

#include "TypeList.h"

void
test1()
{
    std::cout << "Length<int>..." << Length<int>::value << std::endl;
    std::cout << "Length<int, int>..." << Length<int, int>::value << std::endl;
    std::cout << "Length<int, int, char>..." << Length<int, int, char>::value << std::endl;

    std::cout << "********************************" << std::endl;
    std::vector<int> IntVector;

    std::cout << typeid(TypeAt<1, int, std::string, std::vector<int> >::Result).name() << std::endl;
    std::cout << typeid(TypeAt<2, int, std::string, std::vector<int> >::Result).name() << std::endl;
    std::cout << typeid(TypeAt<3, int, std::string, std::vector<int> >::Result).name() << std::endl;

    TypeAt<1, int, std::string, std::vector<int> >::Result var1;
    var1 = 10;
    
    TypeAt<2, int, std::string, std::vector<int> >::Result var2;
    var2 = "ssss";

    TypeAt<3, int, std::string, std::vector<int> >::Result var3;
    var3 = IntVector;
}

int
main( int argc, char *argv[] )
{
    test1();

    return 0;
}

3,执行结果

如何使用C++模板变参实现Loki中的Length和TypeAt

TypeAt的实现中,由于变参要位于最后一个参数,故把位置参数移到了模板第一个参数。

关于“如何使用C++模板变参实现Loki中的Length和TypeAt”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。