Make qSwap() noexcept, if possible

This greatly increases the value of qSwap(), since not only does it
automatically do the parallel std+ADL lookup of swap(), but also
now centralizes the rather messy code involved to create a correct
noexcept specification.

Other code now can simply use
    Q_DECL_NOEXCEPT_EXPT(noexcept(qSwap(lhs, rhs))).

Change-Id: Ia35df4876b143e86c4150ac452a48c3775c3702b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Marc Mutz 2015-01-07 15:44:05 +01:00
parent 0dacb1e282
commit 597a71fa99
1 changed files with 12 additions and 0 deletions

View File

@ -802,8 +802,20 @@ static inline bool qIsNull(float f)
# define Q_DUMMY_COMPARISON_OPERATOR(C)
#endif
namespace QtPrivate
{
namespace SwapExceptionTester { // insulate users from the "using std::swap" below
using std::swap; // import std::swap
template <typename T>
void checkSwap(T &t)
Q_DECL_NOEXCEPT_EXPR(noexcept(swap(t, t)));
// declared, but not implemented (only to be used in unevaluated contexts (noexcept operator))
}
} // namespace QtPrivate
template <typename T>
inline void qSwap(T &value1, T &value2)
Q_DECL_NOEXCEPT_EXPR(noexcept(QtPrivate::SwapExceptionTester::checkSwap(value1)))
{
using std::swap;
swap(value1, value2);