Boost C++ Libraries: Ticket #5818: Compile error with BOOST_REQUIRE_THROW https://svn.boost.org/trac10/ticket/5818 <p> I recently post this email on the boost-users mailing list: </p> <p> Hi, I've just found that we have a compiler error when using the BOOST_REQUIRE_THROW macro in a if/else statement with implicit block. Let's have an example: </p> <p> BOOST_AUTO_TEST_CASE(foo) { </p> <blockquote> <p> if(true) </p> <blockquote> <p> BOOST_REQUIRE_THROW(throw std::exception(), std::exception); </p> </blockquote> <p> else </p> <blockquote> <p> BOOST_REQUIRE_THROW(throw std::exception(), std::exception); </p> </blockquote> </blockquote> <p> } </p> <p> Will fail with: </p> <p> file.cpp: In member function ‘void foo::test_method()’: file.cpp:184:2: error: ‘else’ without a previous ‘if’ </p> <p> To make it work, we need to put explicit block: </p> <p> BOOST_AUTO_TEST_CASE(foo) { </p> <blockquote> <p> if(true) { </p> <blockquote> <p> BOOST_REQUIRE_THROW(throw std::exception(), std::exception); </p> </blockquote> <p> } else { </p> <blockquote> <p> BOOST_REQUIRE_THROW(throw std::exception(), std::exception); </p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> Wouldn't it be possible to add this block in the macro so library users don't have to care about? </p> <p> And got this answer: </p> <p> BOOST_REQUIRE_THROW expands into a try { ... } catch { ... } block. Such a block is a valid single statement, but if you put a ; at the end of the catch block, that semicolon terminates the if statement. [...] The macro could be enclosed in a do { .... } while (0) construct in order to make it syntactically appear more like a single statement that is terminated with a semicolon. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5818 Trac 1.4.3 Gennadiy Rozental Tue, 18 Oct 2011 09:50:00 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5818#comment:1 https://svn.boost.org/trac10/ticket/5818#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in trunk </p> Ticket