Home > database >  What happens to the old one after swapping with an unnamed object in C
What happens to the old one after swapping with an unnamed object in C

Time:09-21

Consider the statement decltype(s){}.swap(s), where s is a STL class entity. If s is not ::std::array, this gets a nice complexity ( O(constant) ).

But I wonder, where the old one goes? Is it automatically deleted?

I think so, for the old one will never be used again. But I'm not sure about that (if the compiler will think so).

CodePudding user response:

The content of s is swapped with the content of the temporary object created by decltype(s){}. Effectively, s will simply become re-initialized with its default content, and the temporary object will destroy the old content when itself is destroyed when it goes out of scope.

And FYI, swap() works with std::array, too.

  • Related