template parameter packs access Nth type and Nth element

后端 未结 5 991
广开言路
广开言路 2020-11-28 07:59

The following paper is the first proposal I found for template parameter packs.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf

At page 16,

5条回答
  •  我在风中等你
    2020-11-28 08:22

    Access N-th element?

    Using std::forward_as_tuple:

    template 
    decltype(auto) get(Ts&&... ts) {
      return std::get(std::forward_as_tuple(ts...));
    }
    

    Example usage:

    template
    void foo(Ts&&...ts){
    
      auto& first = get<0>(ts...);
      auto second = get<1>(ts...);
    
      first = 'H';
      second = 'E';
    
      (std::cout << ... << ts);
    }
    
    foo('h','e','l','l','o');
    // prints "Hello"
    

    This answer is to supplement Emile Cormier's answer which gives only the n-th type.

提交回复
热议问题