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

前端 未结 2 856
一整个雨季
一整个雨季 2020-12-14 19:09

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 ther

2条回答
  •  悲哀的现实
    2020-12-14 19:42

    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 
    #include 
    
    template 
    struct first
    {
        typedef T1 type;
    };
    
    template 
    struct last
    {
        typedef typename last::type type;
    };
    
    template 
    struct last
    {
        typedef T1 type;
    };
    
    template 
    struct A
    {
        typedef typename first::type first;
        typedef typename last::type  last;
    };
    
    struct B1 {};
    struct B2 {};
    struct B3 {};
    
    int main()
    {
        typedef A T;
        std::cout << typeid(T::first).name() << '\n';
        std::cout << typeid(T::last).name() << '\n';
    }
    

提交回复
热议问题