Add nice error messages for unsupported && and ||

As explained in issue #1273, `operator&&` and `operator||` should give
a proper compile time error on use instead of the compiler complaining
about them not being defined. This commit adds an `always_false` type in
`catch_meta.hpp` used for implementing a nice `static_assert` for both
of the abovementioned operators.

Closes #1273
This commit is contained in:
BiCapitalization 2018-12-30 23:44:20 +01:00 committed by Martin Hořeňovský
parent e7fce90b49
commit b3faceede2
2 changed files with 20 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include "catch_tostring.h" #include "catch_tostring.h"
#include "catch_stringref.h" #include "catch_stringref.h"
#include "catch_meta.hpp"
#include <iosfwd> #include <iosfwd>
@ -143,6 +144,20 @@ namespace Catch {
return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs }; return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs };
} }
template<typename RhsT>
auto operator && ( RhsT const& ) -> BinaryExpr<LhsT, RhsT const&> const {
static_assert(always_false<RhsT>::value,
"operator&& is not supported inside assertions, "
"wrap the expression inside parentheses, or decompose it");
}
template<typename RhsT>
auto operator || ( RhsT const& ) -> BinaryExpr<LhsT, RhsT const&> const {
static_assert(always_false<RhsT>::value,
"operator|| is not supported inside assertions, "
"wrap the expression inside parentheses, or decompose it");
}
auto makeUnaryExpr() const -> UnaryExpr<LhsT> { auto makeUnaryExpr() const -> UnaryExpr<LhsT> {
return UnaryExpr<LhsT>{ m_lhs }; return UnaryExpr<LhsT>{ m_lhs };
} }

View File

@ -9,6 +9,8 @@
#ifndef TWOBLUECUBES_CATCH_META_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_META_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_META_HPP_INCLUDED #define TWOBLUECUBES_CATCH_META_HPP_INCLUDED
#include <type_traits>
template< typename... > template< typename... >
struct TypeList{}; struct TypeList{};
@ -73,4 +75,7 @@ struct combine
}; };
}; };
template<typename T>
struct always_false : std::false_type {};
#endif // TWOBLUECUBES_CATCH_META_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_META_HPP_INCLUDED