Boost logo

Boost Users :

Subject: Re: [Boost-users] Metaprogramming Question
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2012-02-11 20:39:08

On Sat, 11 Feb 2012, John M. Dlugosz wrote:

> On 2/10/2012 1:46 PM, Michael Schulze wrote:
>> That is not a bug of gcc. It is right if it states that the
>> interpretation could be done in two way either the else belongs to the
>> first if or to the second one. However, you can make yourself and the
>> compiler happy. Try the following.
>> #define MY_FANCY_ASSERT(cond, str) \
>> do { if (cond) {} else format(str) % __FILE__ % __LINE__ } while(0)
>> With this you have encapsulated the if-else statement and the compiler
>> has now no reasons to complain anymore.
> How is that different from leaving off the do and while and just putting
> braces around the whole thing?
>> #define MY_FANCY_ASSERT(cond, str) \
>> { if (cond) {} else format(str) % __FILE__ % __LINE__ }

The do-while formulation requires the user to provide a semicolon after
the macro call, while a normal set of braces does not. The do-while also
fixes problems such as:

if (p) MY_FANCY_ASSERT(...); else bar();

which is a syntax error since the first semicolon terminates the if
statement, preventing the presence of an else clause later.

-- Jeremiah Willcock

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at