freetype-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ft-devel] MSVC build woes !


From: J. Ali Harlow
Subject: Re: [ft-devel] MSVC build woes !
Date: Tue, 11 Jun 2013 16:17:17 +0100

On Tue, Jun 11, 2013, at 03:56 PM, John Emmas wrote:
> On 11/06/2013 15:14, Robin Watts wrote:
> >
> > There is a VERY good reason for the while being there. I will try to 
> > explain:
> >
> > Imagine that you have the following code:
> >
> >   if (x == 0)
> >       FT_ASSERT(some_condition);
> >   else
> >       return 42;
> >   return 23;
> >
> > Without the while loop, the code would expand to:
> >
> >    if (x == 0)
> >       if (!some_condition)
> >           FT_Panic( ... );
> >       else
> >           return 42;
> >    return 23;
> >
> > (indentation changed for clarity)
> >
> > i.e. the behaviour of the code is changed for x == 0;
> >
> 
> Thanks for the explanation Robin,
> 
> That being the case, this solution works for MSVC:-
> 
>          #define FT_ASSERT( condition )                              \
>                {                                \
>                      if ( !( condition ) ) 
>                                   \
>                        FT_Panic( "assertion failed on line %d of file 
> %s\n", \
>                                  __LINE__, __FILE__ ); 
>                        \
>                }
> 
> In fact, a similar strategy is already being used in the definition of  
> 'FT_THROW' so I guess it should work for other compilers too.

That's not good either:

FT_ASSERT(some_condition);

results in two statements which means that Robin's example:

> >   if (x == 0)
> >       FT_ASSERT(some_condition);
> >   else
> >       return 42;
> >   return 23;

won't work. Better is:

#define FT_ASSERT( condition )                              \
                      if ( !( condition ) ) 
                                   \
                        FT_Panic( "assertion failed on line %d of file 
 %s\n", \
                                  __LINE__, __FILE__ ); 
                        else \

which at least results in correct code if FT_ASSERT is used correctly,
but can fail in unexpected ways if you forget to put the semi-colon
after the macro call. There really is a very good reason why do ...
while(0) is standard.

Ali.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]