Static assertion for && and || expressions

This commit is contained in:
Phil Nash 2013-06-29 12:07:33 +01:00
parent a9b346b14d
commit 5d26ca1af7
3 changed files with 10 additions and 12 deletions

View File

@ -13,8 +13,6 @@
namespace Catch { namespace Catch {
struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
// Wraps the LHS of an expression and captures the operator and RHS (if any) - wrapping them all // Wraps the LHS of an expression and captures the operator and RHS (if any) - wrapping them all
// in an ExpressionResultBuilder object // in an ExpressionResultBuilder object
template<typename T> template<typename T>
@ -76,6 +74,8 @@ public:
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator - ( RhsT const& ); template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator - ( RhsT const& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator / ( RhsT const& ); template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator / ( RhsT const& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator * ( RhsT const& ); template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator * ( RhsT const& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator && ( RhsT const& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator || ( RhsT const& );
private: private:
template<Internal::Operator Op, typename RhsT> template<Internal::Operator Op, typename RhsT>

View File

@ -16,6 +16,8 @@
namespace Catch { namespace Catch {
struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
// Wraps the (stringised versions of) the lhs, operator and rhs of an expression - as well as // Wraps the (stringised versions of) the lhs, operator and rhs of an expression - as well as
// the result of evaluating it. This is used to build an AssertionResult object // the result of evaluating it. This is used to build an AssertionResult object
class ExpressionResultBuilder { class ExpressionResultBuilder {
@ -43,6 +45,9 @@ public:
AssertionResult buildResult( AssertionInfo const& info ) const; AssertionResult buildResult( AssertionInfo const& info ) const;
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator && ( RhsT const& );
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator || ( RhsT const& );
private: private:
AssertionResultData m_data; AssertionResultData m_data;
struct ExprComponents { struct ExprComponents {

View File

@ -33,9 +33,7 @@ TEST_CASE
{ {
std::pair<int, int> aNicePair( 1, 2 ); std::pair<int, int> aNicePair( 1, 2 );
// !TBD: would be nice if this could compile without the extra parentheses REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair );
REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair );
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -45,13 +43,8 @@ TEST_CASE
"Where the is more to the expression after the RHS" "Where the is more to the expression after the RHS"
) )
{ {
/* // int a = 1, b = 2;
int a = 1; // REQUIRE( a == 2 || b == 2 );
int b = 2;
// This only captures part of the expression, but issues a warning about the rest
REQUIRE( a == 2 || b == 2 );
*/
WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" ); WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" );
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////