I have several classes that are based on the PIMPL idiom (where a unique_ptr
refers to the actual implementation struct).
I haven't added a friend
swap
function (as described here) as, to my knowledge, the standard std::swap
uses move-semantics which would nicely swap out the unique_ptr
s. So far, so good.
However, I read (the somewhat outdated Effective C++
from Scott Meyers that says in Item 25:
However, the default swap implementation might not thrill you. It involves copying three objects: a to temp, b to a, and temp to b. [...] For some types, the default swap puts you on the fast track to the slow lane. Foremost among such types are those consisting primarily of a pointer to another type that contains the real data. A common manifestation of this design is the "pimpl" idiom.
After which he also suggest to specialize std::swap
as well.
My question is whether this still holds in C++11. It seems that the C++11 swap
works just fine for pimpl'd classes. I understand that adding a friend
swap
allows the STL to use ADL and so on, but I prefer to keep my classes as lean as possible.
Aucun commentaire:
Enregistrer un commentaire