From the C++ Truths blog, with a creative use of std::tuple
:
A tale of noexcept swap for user-defined classes in C++11
by Sumant Tambe
From the article:
I like my strong exception safety in my little copy-assignment operator so I don’t want my
swap
to throw but I don’t want my program tostd::terminate()
if an exception is really thrown. With all that in mind, I would rewrite theswap
as follows...This is not unlike what’s proposed by others but there’s more. The static assert looks awful and looks redundant. There is already a standard library utility that does the same thing:
std::tuple
. As mentioned before,std::tuple
’sswap
throws if any member swap throws. We use it here to make our job a lot easier...
Add a Comment
Comments are closed.