swap temporary tuples of references

前端 未结 3 1524
执笔经年
执笔经年 2021-01-04 10:42

I\'m writing a custom iterator that, when dereferenced returns a tuple of references. Since the tuple itself is ephemeral, I don\'t think I can return a reference from oper

3条回答
  •  星月不相逢
    2021-01-04 10:49

    The answer I came up with was to write a tuple wrapper class:

    template
    struct tupleWrapper{
       std::tuple data;
       tupleWrapper(std::tuple _data) : data{_data}
       {}
       operator std::tuple () {return data;}
       std::tuple& asTuple() {return data;}
     };
    
    template
    void swap(tupleWrapper t1, tupleWrapper t2){
      std::swap(t1.data, t2.data);
    }
    

    And a get function that can be found with ADL, since the conversion operator doesn't get called when doing TAD for std::get.

    template
      auto get(tupleWrapper tw)->decltype(std::get(tw.data)){
        return std::get(tw.data);
    }
    

    It's not ideal, but I think it will work well enough.

提交回复
热议问题