← Back to team overview

dolfin team mailing list archive

Re: A general comment on bugs with uninitialized vectors

 

On Tue, Mar 25, 2008 at 03:08:40PM +0100, Martin Sandve Alnæs wrote:
> 2008/3/25, Martin Sandve Alnæs <martinal@xxxxxxxxx>:
> > 2008/3/25, Johan Hake <hake@xxxxxxxxx>:
> >
> > > On Tuesday 25 March 2008 13:38:22 Martin Sandve Alnæs wrote:
> >
> > >  > But what's the point of the "do { ... } while(false);" ??
> >  >  > { ... } or just ... should be the same thing as far as I can see.
> >  >
> >  >
> >  > It's because macros are evil! :)
> >  >
> >  >  http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.4
> >  >
> >  >  It looks like macros with if statements have a risk of nesting with the wrong
> >  >  if, if it is used inside an other if statement. (Nice sentence! :) )
> >
> >
> > Yes, but a simple {} should do the trick to define a local scope. No
> >  need for do-while.
> 
> 
> I stand corrected... Turns out this was the "naive solution", here's
> the explanation:

ok, so we are doing something clever! I think Jim Tilander helped me
with this originally so he should get the credit. Nice!

-- 
Anders


> 
> The naive solution is to wrap the statements inside {...}, such as this:
> 
>  #define MYMACRO(a,b) \                (Bad)
>      { \
>          statement1; \
>          statement2; \
>          ... \
>          statementN; \
>      }
> 
> But this will cause compile-time errors with things like the following:
> 
>  if (whatever)
>      MYMACRO(foo, bar);
>  else
>      baz;
> 
> since the compiler will see a } ; else which is illegal:
> 
>  if (whatever)
>  {
>      statement1;
>      statement2;
>      ...
>      statementN;
>  };  // ERROR: can't have }; before else
>  else
>      baz;
> 
> 


References