|
Boost Users : |
Subject: Re: [Boost-users] [Boost][Test] Compile error with BOOST_REQUIRE_THROW
From: Richard (legalize+jeeves_at_[hidden])
Date: 2011-08-28 13:06:00
[Please do not mail me a copy of your followup]
boost-users_at_[hidden] spake the secret code
<4E583EC5.7080700_at_[hidden]> thusly:
>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â
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.
If you want to write code this way, then don't put a semicolon after
the BOOST_REQUIRE_THROW macro.
>To make it work, we need to put explicit block:
That's one way to make it work, and its one reason why always using
{}s for control structures is recommended when those control
structures contain macros.
>Wouldn't it be possible to add this block in the macro so library users
>don't have to care about?
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. File a bug report on the boost
bug tracker.
-- "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/> Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net