get part of std::tuple

前端 未结 7 1227
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 23:09

I have a tuple of unknown size (it\'s template parametr of method)

Is it way to get part of it (I need throw away first element of it)

For example, I have

7条回答
  •  忘掉有多难
    2020-12-03 00:08

    There may be an easier way, but this is a start. The "tail" function template returns a copied tuple with all values of the original except the first. This compiles with GCC 4.6.2 in C++0x-mode.

    template
    struct assign {
      template
      static void x(ResultTuple& t, const SrcTuple& tup) {
        std::get(t) = std::get(tup);
        assign::x(t, tup);
      }
    };
    
    template<>
    struct assign<1> {
      template
      static void x(ResultTuple& t, const SrcTuple& tup) {
        std::get<0>(t) = std::get<1>(tup);
      }
    };
    
    
    template struct tail_helper;
    
    template
    struct tail_helper> {
      typedef typename std::tuple type;
      static type tail(const std::tuple& tup) {
        type t;
        assign::value>::x(t, tup);
        return t;
      }
    };
    
    template
    typename tail_helper::type tail(const Tup& tup) {
      return tail_helper::tail(tup);
    }
    

提交回复
热议问题