swap temporary tuples of references

前端 未结 3 1522
执笔经年
执笔经年 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:59

    You can place the std::tuple as a data member and return a reference to that:

    class iterator
    {
    public:
        iterator(std::vector& _v1,
                 std::vector& _v2)
          : tup(_v1[5], _v2[5]) {}
    
        tuple_of_references& operator*() const
        {
            return tup;
        }
    private:
        typedef std::tuple tuple_of_references; // just to cut down size
        tuple_of_references tup;
    };
    

提交回复
热议问题