lmi
[Top][All Lists]
Advanced

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

Re: [lmi] Fall through (was: Suspected stack corruption)


From: Vadim Zeitlin
Subject: Re: [lmi] Fall through (was: Suspected stack corruption)
Date: Fri, 11 Dec 2015 14:43:33 +0100

[Let me address just this part of your message separately:]

On Fri, 11 Dec 2015 13:34:31 +0000 Greg Chicares <address@hidden> wrote:

GC> As for this part:
GC>      if(...
GC>      ...
GC>      else if(...
GC>      ...
GC>          {
GC> -        // else: fall through
GC> +        }
GC> +    else
GC> +        {
GC> +        ; // Fall through.
GC>          }
GC> I figure that it's preferable to express a control structure in code
GC> rather than comments. Perhaps someday we'll use an automated tool
GC> that looks for code smells like 'if...else if...else if' with no
GC> catch-all 'else' at the end.

 I don't know of any tools flagging if/else-if chains without the final
else as an error but, on a tangentially related topic, there are existing
tools (clang) that warn about implicit fall-through in switch statements
and this is the reason there is wxFALLTHROUGH macro in wxWidgets: it allows
you to write

        switch ( one_or_the_other ) {
                case One:
                        // Do something
                        wxFALLTHROUGH();

                case TheOther:
                        // Do something more
                        break;
        }

without getting warnings and clearly indicating the intent of the code.

 And while it's hard to do it in this particular case (it would be simpler
with C++11...), generally speaking I prefer using switch() to chains of
if() statements as you can rely on the compiler checking the completeness
for you as both clang and g++ will also warn you about the missing cases if
you switch over an enum. This still doesn't make C++ switch() as nice as
pattern matching in some other languages, but it gets a little bit closer
to it.

 Regards,
VZ

reply via email to

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