In my main I have the following instructions:
message("std::swap");
std::swap(*a, *b);
The strc class has the following attribute:
Char * data;
Then I put the constructors that use swap below.
strc::strc(strc && o) {
data = std::move(o.data);
o.data = nullptr;
msg("move ctor");
}
strc::~strc() {
msg("dtor");
delete[] data;
}
strc & strc::operator = (strc o) {
std::cout<<o.data;
swap(o);
msg("copy and swap =");
return *this;
}
const char * strc::value() const {
return data;
}
strc::operator const char * () const {
return value();
}
void strc::swap(strc & o) {
std::swap(this->data, o.data);
}
This is the output to see calls made to builders when you called swap.
std::swap
strc: move ctor (one)
strc: move ctor (two)
strc: copy and swap = (two)
strc: dtor
strc: move ctor (one)
strc: copy and swap = (one)
strc: dtor
strc: dtor
Someone could explain to me the internal procedure that they follow. In move ctor, I understand that it returns an instance where its "data" is the string of a and b in the respective calls, but when the constructor = calls swap (strc & o) I do not know where it is saving data, if in principle I had already saved it in the instance in the move constructor. I'm lost. If someone can explain to me the whole process of std :: swap (* a, * b) and what he does with each call to the constructors.
I put it in debug mode, I see that it moves
#endif
{
// concept requirements
__glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
_Tp __tmp = _GLIBCXX_MOVE(__a);
__a = _GLIBCXX_MOVE(__b);
__b = _GLIBCXX_MOVE(__tmp);
}
template<typename _Tp>
constexpr typename std::remove_reference<_Tp>::type&&
move(_Tp&& __t) noexcept
{ return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
But I get lost trying to follow it because it creates _t pointers and directions that I can not understand.
Greetings