A tale of noexcept swap for user-defined classes in C++11 -- Sumant Tambe

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 to std::terminate() if an exception is really thrown. With all that in mind, I would rewrite the swap 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’s swap throws if any member swap throws. We use it here to make our job a lot easier...

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.