5 ms·
If you're using gcc, you can compile with "-Wshadow" to get a warning for this type of problem. See: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html htt
by pagekalisedown 15y ago
If you're using gcc, you can compile with "-Wshadow" to get a warning for this type of problem.
See:
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
- silentbicycle 15y agoDefinitely. Know your warning options! Also, try using multiple compilers. tcc compiles very quickly, clang often has better error messages, etc. Speaking of variable shadowing: It's usually worth wrapping any preprocessor macros in a "do { ... } while (0)" block unless you deliberately want variable definitions to escape (in which case, token pasting a suffix is usually a good idea).
- AlexandrB 15y agoIsn't "do { ... } while (0)" the same as just "{ ... }"? I sometimes use "{ ... }" to limit the scope of a variable that is only needed for a small section of code.
- enqk 15y agodo { ... } while (0) makes your macro behave like a statement (i.e. the semicolon is mandatory)
- rcfox 15y ago({ ... }) does this too. (Is this GCC-specific?)
- mansr 15y agoThat is a gcc extension. The construct as a whole has the value of the last statement executed within. Usually an inline function is preferable since it achieves the same thing using only standard syntax.
- silentbicycle 15y agoYes. I don't include the trailing semicolon in macro definitions, because I expect the macro to be followed by them: MACRO(foo);
- huhtenberg 15y agoFormer lets break'ing out, latter doesn't.
- mikeash 15y agoWould be nice if there was one which would warn for this specific case but not shadowing in general, which I occasionally like. (This probably makes me a bad person.)
- kstenerud 15y agoSeeing a variable shadow something in an outer scope makes me cringe, simply because the decrease in readability far outweighs any benefit it could give. You might remember how things are, but the poor sap who has to keep extra scoping depths in his mind just to maintain your code will curse your name every day.
- ajross 15y agoIt certainly doesn't make you a bad person. If lexical scoping wasn't the intent of the language authors, it wouldn't exist. I tend to agree that the warning is more of a hassle than it's worth. Though I guess it can be argued that if your scoping is so deep that you actually need to reuse names, that perhaps some refactoring should be in order.