by ot · 15y ago · 0 comments
CHECK(a == b);
the expression-capturing part of the code is something like (these are not the actual names) #define CAPTURE(expr) SomeStrangeObject() ->* expr
which for a == b expands to something like SomeStrangeObject() ->* a == b
Since ->* has highest priority and is seldom overloaded, this is actually parsed by the compiler as (SomeStrangeObject() ->* a) == b
The subexpression in parentheses returns an object which wraps a and overloads the comparison operators (==, >, < ...), so that it can capture also the comparison operator used and b.While very convenient it is kind of easy to "break". For example
int one = 2;
CHECK( one == 1 );
CHECK( (one == 1) );
the two checks are seemingly the same, but in the second case the trick doesn't work: my_test.cpp(6): one == 1 failed for: 2 == 1
my_test.cpp(7): (one == 1) failed for: 0
Also, expressions not in the '<operand> <comparison operator> <operand>' form are not supported, for example CHECK ( one + 1 == 2 );
causes this obscure error message my_test.cpp:10: error: no match for ‘operator+’ in ‘Catch::ResultBuilder(((const char*)"one + 1 == 2"), 0, ((const std::string&)(& std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"my_test.cpp"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))), 10u, ((const std::string&)(& std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"CHECK"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>()))))))).Catch::ResultBuilder::operator->* [with T = int](((const int&)((const int*)(& one)))) + 1’
Same for CHECK( 1 == 1 && one == 1);
which fails with my_test.cpp:9: error: no match for ‘operator&&’ in ‘((Catch::ResultBuilder*)Catch::ResultBuilder(((const char*)"1 == 1 && one == 1"), 0, ((const std::string&)(& std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"my_test.cpp"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))), 9u, ((const std::string&)(& std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"CHECK"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>()))))))).Catch::ResultBuilder::operator->* [with T = int](((const int&)((const int*)(&1)))))->Catch::ResultBuilder::operator== [with RhsT = int](((const int&)((const int*)(&1)))) && (one == 1)’
my_test.cpp:9: note: candidates are: operator&&(bool, bool) <built-in>
Not sure if these issues can cause real problems in everyday use, but they may cause headaches if one doesn't want to dig into preprocessor/template metaprogramming code.Don't know if it is relevant, but I think you can also work around the parentheses problem using some preprocessor magic. Boost Preprocessor does something similar in its handling of lists, but I haven't been using it for a very long time and I don't remember how it did the trick.