如何检测在可变参数模板第一个和最后一个参数? [英] How to detect the first and the last argument in the variadic templates?

查看:130
本文介绍了如何检测在可变参数模板第一个和最后一个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测在可变参数模板第一个和最后一个参数?

对于第一个参数很容易(只比较 ...的sizeof(T) 0),但有一种方法来检测的最后一个元素?

这个例子:

 的#include<&iostream的GT;
#包括LT&;&所属类别GT;模板<类... T>
一个结构
{
    INT美孚(INT K){返回K表; };
};模板<组别为T1,类... T>
结构A< T1,T ...>
{
    A():A()
    {
        性病::法院LT&;<A i =<< ...的sizeof(T)LT;<的std :: ENDL
                 <<一类=<< typeid的(T1)。名称()<<的std :: ENDL;
    }    INT美孚(INT K){返回anotherA.foo(a.foo(K)); };    T1一个;
    A<的...> anotherA;
};结构B1
{
    B1(){性病::法院LT&;<B1<<的std :: ENDL; };
    INT美孚(INT K){性病::法院LT&;<B1 :: foo的()K =<< K<<的std :: ENDL;回归K + 1; };
};
结构B2
{
    B2(){性病::法院LT&;<B2<<的std :: ENDL; };
    INT美孚(INT K){性病::法院LT&;<B2 :: foo的()K =<< K<<的std :: ENDL;回归K + 2; };
};
结构B3
{
    B3(){性病::法院LT&;<B3<<的std :: ENDL; };
    INT美孚(INT K){性病::法院LT&;<B3 :: foo的()K =<< K<<的std :: ENDL;回归K + 3; };
};诠释的main()
{
    A< B3,B2,B1>一个;    性病::法院LT&;<值
             &所述;&下; a.foo(5)
             <<的std :: ENDL;
}


解决方案

我不是正面的,如果这是你想要的。但这里有两个名为公用事业第一末页称取可变参数模板和typedef的分别在第一和最后一类:

 的#include<&iostream的GT;
#包括LT&;&所属类别GT;模板<组别为T1,类... T>
首先结构
{
    的typedef T1型;
};模板<组别为T1,类... T>
结构持续
{
    的typedef typename的最后<吨...> ::类型类型;
};模板<组别为T1>
结构持续< T1>
{
    的typedef T1型;
};模板<类... T>
一个结构
{
    的typedef typename的第一<吨...> ::型第一;
    的typedef typename的最后<吨...> ::类型最后;
};结构B1 {};
结构B2 {};
结构B3 {};诠释的main()
{
    typedef的A< B1,B2,B3>笔;
    性病::法院LT&;< typeid的(T ::第一)。名称()<<的'\\ n';
    性病::法院LT&;< typeid的(T ::最后一个)。名称()<<的'\\ n';
}

How to detect the first and the last argument in the variadic templates?

For the 1st argument it is easy (just compare sizeof...(T) with 0), but is there a way to detect the last element?

The example :

#include <iostream>
#include <typeinfo>

template < class... T >
struct A
{
    int foo(int k){ return k; };
};

template < class T1, class... T >
struct A< T1, T... >
{
    A() :a()
    {
        std::cout<<"A  i="<<sizeof...(T)<<std::endl
                 <<"   a type = " << typeid(T1).name()<<std::endl;
    }

    int foo(int k){ return anotherA.foo( a.foo(k) ); };

    T1 a;
    A< T... > anotherA;
};

struct B1
{
    B1(){ std::cout<<"b1"<<std::endl; };
    int foo(int k){ std::cout<<"b1::foo() k="<<k<<std::endl; return k+1; };
};
struct B2
{
    B2(){ std::cout<<"b2"<<std::endl; };
    int foo(int k){ std::cout<<"b2::foo() k="<<k<<std::endl; return k+2; };
};
struct B3
{
    B3(){ std::cout<<"b3"<<std::endl; };
    int foo(int k){ std::cout<<"b3::foo() k="<<k<<std::endl; return k+3; };
};

int main ()
{
    A< B3, B2, B1 > a;

    std::cout<<"the value is "
             <<a.foo(5)
             << std::endl;
}

解决方案

I'm not positive if this is what you want. But here are two utilities named first and last that take variadic templates and typedef the first and last type respectively:

#include <iostream>
#include <typeinfo>

template <class T1, class ...T>
struct first
{
    typedef T1 type;
};

template <class T1, class ...T>
struct last
{
    typedef typename last<T...>::type type;
};

template <class T1>
struct last<T1>
{
    typedef T1 type;
};

template <class ...T>
struct A
{
    typedef typename first<T...>::type first;
    typedef typename last<T...>::type  last;
};

struct B1 {};
struct B2 {};
struct B3 {};

int main()
{
    typedef A<B1, B2, B3> T;
    std::cout << typeid(T::first).name() << '\n';
    std::cout << typeid(T::last).name() << '\n';
}

这篇关于如何检测在可变参数模板第一个和最后一个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆