Saturday 28 October 2017

c++ - Calling std::swap in shared pointers' values call a bunch of constructors and destructors

I have recently started learning move semantics and shared
pointers, and I am having a lot of difficulty trying to understand
it.



I am currently taking a course on these
topics, but the instructor did not explain why these constructors and destructors are
called when we swap the values contained in the pointers. The calls to swap in the
shared pointer (as in b.swap(a)) and the std::swap(a, b) are not calling any
constructors or destructor, while the call to std::swap(*a, *b) are calling a bunch of
them.



The main
code:



int main(int argc, char**
argv){

std::shared_ptr a =
std::make_shared("one");

std::shared_ptr b
= std::make_shared("two");

message("b.swap(a)"); // No
constructors or destructors are called.
b.swap(a);

disp(a);
disp(b);

message("std::swap"); // A bunch of
constructors and destructors are called.
std::swap(*a, *b);

disp(a);

disp(b);

message("std::swap"); //
No constructor or destructors are called.
std::swap(a, b);

disp(a);
disp(b);

return
0;
}



The
implemented class for "strc" is (I am just going to show the 'important' implementations
in it for the sake of
conciseness):



strc::strc(strc
&& o){
msg("Move Constructor.");
this->data =
std::move(o.data);
o.data =
nullptr;
}

strc::~strc(){


msg("Destructor.");
delete [] data;
}

strc
& strc::operator = (strc o){
msg("Copy and Swap (=).");

swap(o);
return *this;
}


void
strc::swap(strc & o){
msg("Calling std::swap");

std::swap(this->data,
o.data);
}


This
is what is being printed to the console (For me to understand more about what is
happening and have a thorough understanding of move semantics and shared
pointers).



Call to
b.swap(a)

one (1)

two (1)



Call to std::swap(*a,
*b)

strc: Move Constructor.
strc: Move
Constructor.
strc: Copy and Swap (=).
strc: Calling
std::swap
strc: Destructor.
strc: Move Constructor.
strc:
Copy and Swap (=).

strc: Calling std::swap
strc:
Destructor.
strc: Destructor.
two (1)
one (1)



Call to std::swap(a,
b)

one (1)
two (1)




Why is that? Does it have to do with
the move semantics? Shouldn't it be calling the standard swap function? I cannot
understand the difference between these swap calls, and why one of them is calling all
of these constructors and destructor.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...