Is there an elegant way to swap references in C++?

大城市里の小女人 提交于 2019-12-04 00:27:51

问题


Sometimes classes are referencing other classes. Implementing std::swap() for such classes cannot be straightforward, because it would lead to swapping of original instances instead of references. The code below illustrates this behavior:

#include <iostream>

class A
{
   int& r_;
public:
   A(int& v) : r_(v) {}
   void swap(A& a)
   {
      std::swap(r_, a.r_);
   }
};

void test()
{
   int x = 10;
   int y = 20;

   A a(x), b(y);
   a.swap(b);

   std::cout << "x=" << x << "\n"
             << "y=" << y << "\n";
}

int main()
{
    test();
    return 0;
}

A simple workaround with a union:

class A
{
   union
   {
      int& r_;
      size_t t_;
   };
public:
   A(int& v) : r_(v) {}
   void swap(A& a)
   {
      std::swap(t_, a.t_);
   }
};

This is effective, but not handsome. Is there a nicer way to swap two references in C++? Also how does C++ standard explain mixing references and values in one union, considering that in Stroustrup's "The C++ Programming Language" book a 'reference' is defined as an 'alternative name of an object, an alias' (p.189).


回答1:


The union trick you're using is about as non-portable as code gets. The standard places no requirements whatsoever on how compilers implement references. They can (and most probably do) use pointers under the hood, but this is never guaranteed. Not to mention the fact that sizeof(size_t) and sizeof(T*) aren't required to be equal anyway.

The best answer to your problem is: don't use reference members if you need an assignable/swappable class. Just use a pointer member instead. After all, references are non-reseatable by definition, yet by wanting the class swappable, you want something reseatable. And that's a pointer.




回答2:


You may use std::reference_wrapper instead of direct reference (which you cannot swap) or pointer (which may be nullptr). Something like:

class A
{
    std::reference_wrapper<int> r;
public:
   A(int& v) : r(v) {}
   void swap(A& rhs)
   {
      std::swap(r, rhs.r);
   }

   int& get() const { return r; }
};

Live example




回答3:


Your program using union is not legal, as you assign the reference but swap the integer than intend to use the reference again. You can read about why, here: Accessing inactive union member and undefined behavior?

An easy solution here is to use pointers instead of references. Then everything will work smoothly, with no special code. If you really don't like this, perhaps you can use boost::optional<int&> instead of plain references, but I would struggle to see how that'd be better.




回答4:


Essentially what you would like to do, if I understand your example, is to rebind references. This you cannot do in C++. You can either swap the values in the referred to objects, or you can use pointers.



来源:https://stackoverflow.com/questions/25804844/is-there-an-elegant-way-to-swap-references-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!