effective way to select last parameter of variadic template

后端 未结 8 902
温柔的废话
温柔的废话 2020-12-01 12:37

I know how to select first parameter of variadic template:

template< class...Args> struct select_first;
template< class A, class ...Args> struct          


        
8条回答
  •  爱一瞬间的悲伤
    2020-12-01 13:10

    Sorry for being a bit late to the party, but I just ran across the same problem , looked up for an answer, didn't like what I see here and realised it can be done using a tuple. Please see C++11 implementation below. Note: one can also get access to an Nth type of a variadic template this way. (The example doesn't check that N exceeds the number of variadic arguments , however the check can be done with SFINAE technique (enable_if) for instance) Is that an acceptable answer or I'm missing anything in the question?

    #include 
    #include 
    
    struct A
    {
        char ch = 'a';
    };
    struct B
    {
        char ch = 'b';
    };
    struct C
    {
        char ch = 'c';
    };
    
    
    template 
    struct SomeVariadic {
    
        using TypesTuple = std::tuple;
    
        using LastType = typename std::tuple_element::type;
    
        template 
        using NthType = typename std::tuple_element::type;
    };
    
    
    
    int main(int argc, char* argv[]) {
    
        SomeVariadic::LastType l;
    
        std::cout << SomeVariadic::LastType().ch << " "
                << SomeVariadic::NthType<1>().ch<< std::endl;
    }
    

提交回复
热议问题