C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 交换引用的临时元组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个自定义迭代器,当dereferenced返回一个引用元组时.由于元组本身是短暂的,我不认为我可以从operator *()返回引用.我认为我的迭代器在语义上是有意义的,因为它具有引用语义,即使operator *返回一个值.

问题是,当我尝试调用std :: swap(或者更确切地说,当std :: sort执行时),如下所示,我得到错误,因为交换需要l值.这个问题有一个简单的解决方法吗?

#include <vector>

class test {
  public:
  test()
    :v1(10),v2(10)
  {}

  class iterator {
    public:
    iterator(std::vector<int>& _v1,std::vector<int>& _v2)
      :v1(_v1),v2(_v2){}

    std::tuple<int&,int&> operator*(){
      return std::tuple<int&,int&>{v1[5],v2[5]};
    }
    std::vector<int>& v1;
    std::vector<int>& v2;
  };

  std::vector<int> v1,v2;
};



int main(){
  test t;
  //error,Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_Traits:3003:1: note: candidate function [with _Tp = std::__1::tuple<int &,int &>] not viable: expects an l-value for 1st argument

  //deep within the bowels of std::sort ...
  std::swap(*(test::iterator(t.v1,t.v2)),*(test::iterator(t.v1,t.v2)));
}

解决方法

rare occasions,可能需要获得临时的左值引用.这很容易通过与std :: move相反的转换来实现:
template <typename T>
T & stay(T && t) { return t; }

用法

std::swap(stay(foo()),stay(bar()));

正如您已经说过的,如果您无法更改呼叫站点,您最好的选择可能是编写自己的参包装并使用ADL:

namespace detail
{
    struct IntRefPair
    {
        int & a,& b;
        IntRefPair(int & x,int & y) : a(X),b(y) {}
    };

    void swap(IntRefPair && lhs,IntRefPair && rhs)
    { 
        std::swap(lhs.a,rhs.a);
        std::swap(lhs.b,rhs.b);
    }
}

// ...

IntRefPair operator*() { return IntRefPair(v1[5],v2[5]); } }

大佬总结

以上是大佬教程为你收集整理的c – 交换引用的临时元组全部内容,希望文章能够帮你解决c – 交换引用的临时元组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。