[Boost-bugs] [Boost C++ Libraries] #5818: Compile error with BOOST_REQUIRE_THROW

Subject: [Boost-bugs] [Boost C++ Libraries] #5818: Compile error with BOOST_REQUIRE_THROW
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-08-28 17:26:44


#5818: Compile error with BOOST_REQUIRE_THROW
-----------------------------------------+----------------------------------
 Reporter: martin.desharnais@… | Owner: rogeeff
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: test
  Version: Boost 1.47.0 | Severity: Cosmetic
 Keywords: BOOST_REQUIRE_THROW |
-----------------------------------------+----------------------------------
 I recently post this email on the boost-users mailing list:

 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:

 BOOST_AUTO_TEST_CASE(foo)
 {
     if(true)
         BOOST_REQUIRE_THROW(throw std::exception(), std::exception);
     else
         BOOST_REQUIRE_THROW(throw std::exception(), std::exception);
 }

 Will fail with:

 file.cpp: In member function ‘void foo::test_method()’:
 file.cpp:184:2: error: ‘else’ without a previous ‘if’

 To make it work, we need to put explicit block:


 BOOST_AUTO_TEST_CASE(foo)
 {
     if(true)
     {
         BOOST_REQUIRE_THROW(throw std::exception(), std::exception);
     }
     else
     {
         BOOST_REQUIRE_THROW(throw std::exception(), std::exception);
     }
 }

 Wouldn't it be possible to add this block in the macro so library users
 don't have to care about?

 And got this answer:

 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.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5818>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC