effective way to select last parameter of variadic template

后端 未结 8 881
温柔的废话
温柔的废话 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:16

    With C++17, the cleanest way is

    template
    struct tag
    {
        using type = T;
    };
    
    template
    struct select_last
    {
        // Use a fold-expression to fold the comma operator over the parameter pack.
        using type = typename decltype((tag{}, ...))::type;
    };
    

    with O(1) instantiation depth.

提交回复
热议问题