Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

c++ - Usage of std::swap() inside move assignment should cause endless recursion (and causes), but it is an example from Stroustrup's book

I'm trying to get the deep knowledge about how should I write my copy and move constructors and assignment operators.

In Bjarne Stroustrup's "The C++ Programming Language - 2013" I see the following example of move constructor and move assignment:

template<class T, class A>
vector_base<T,A>::vector_base(vector_base&& a)
   : alloc{a.alloc},
   elem{a.elem},
   space{a.space},
   last{a.space}
{
   a.elem = a.space = a.last = nullptr; // no longer owns any memory
}

template<class T, class A>
vector_base<T,A>::& vector_base<T,A>::operator=(vector_base&& a)
{
   swap(?this,a);
   return *this;
}

(Side note: there seems to be a typo in the book: ::& should be just &, right?)

I suspected it should cause endless recursion, since std::swap() calls move assignment operator:

template<typename T>
void swap(T& lhs, T& rhs)
{
  auto temp(lhs);
  lhs = std::move(rhs);
  rhs = std::move(temp);
}

I've checked it, here's very simple program:

#include <iostream>
using namespace std;

class TestA {
   int x;

public:
   TestA(int x = 0) : x(x) {
      cout << "TestA value ctor " << x << "
";
   }

   ~TestA() {
      cout << "TestA dtor " << x << "
";
   }

   TestA(const TestA &a) : x(a.x) {
      cout << "TestA copy ctor " << x << "
";
   }

   TestA(TestA &&a) : x(a.x) {
      cout << "TestA move ctor " << x << "
";
   }

   TestA operator=(const TestA &a) {
      x = a.getX();
      cout << "TestA copy assignment " << x << " = " << a.getX() << "
";
      return *this;
   }

   TestA &operator=(TestA &&a) {
      cout << "TestA move assignment " << x << " = " << a.getX() << "
";
      swap(*this, a);
      return *this;
   }

   int getX() const {
      return this->x;
   }

};

int main(void) {
   TestA a{0};
   TestA b{1};
   {
      TestA c{2};
      a = move(c);
   }
}

Which produces the following output, so I was right about endless recursion:

TestA value ctor 0
TestA value ctor 1
TestA value ctor 2
TestA move assignment 0 = 2
TestA move ctor 0
TestA move assignment 0 = 2
TestA move ctor 0
TestA move assignment 0 = 2
TestA move ctor 0
...
...

Do I miss something? How can I use swap() inside move assignment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not missing anything, and the accepted answer is wrong. Stroustrup's book is simply bad. There is no free-standing swap() anywhere in chapter 13; it's strongly implied that this swap() is std::swap() (just like copy() is std::copy(), etc.). It's just a bug. Welcome to C++.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...