5 ms·
if(foo) I avoid doing this because it looks like a function call.
by brianberns 13y ago
if(foo)
I avoid doing this because it looks like a function call.
- kybernetikos 13y agoIn an appropriately powerful language, it could be a function call.
- a1369209993 13y agoafter #define if(X) LET(_it,X) if (_it) it would be a macro call
- yoklov 13y agoI'm fairly certain that shadowing a keyword with a macro is forbidden by the standard (or invokes UB?) but this will still work for most compilers, provided LET is defined reasonably. Hm, something like #define LET(name, X) \ for (int let_once_=1, name=(X); \ let_once_; \ let_once_=0) may be reasonable, though tokenpasting in __LINE__ for good measure might be necessary for nesting.
- 30thElement 13y agoShadowing keywords is perfectly valid, although I believe you can't shadow keywords in standard headers so that compiler writers don't need to worry about it. It gives rise to some "useful" C/C++ features that should never be used, like if you want to access private members in foo.cpp, do #define private public #include "foo.cpp" #undef private
- TazeTSchnitzel 13y agoI can imagine some insidious menace adding this to the top of a file: #define public protected
- yoklov 13y agoI once someone jokingly do (the second only always works in C, not C++) #define else #define struct union Evil!
- TheBoff 13y agoThis is all very well if you have a friendly, high level scripting language like Ruby, but I'm definitely glad I don't have to write a C compiler where functions can take arbitrary blocks of code.
- swift 13y agoApple took care of it for you: http://en.wikipedia.org/wiki/Blocks_(C_language_extension) http://en.wikipedia.org/wiki/Blocks_(C_language_extension)
- rat87 13y ago> In an appropriately powerful language, it could be a function call. > This is all very well if you have a friendly, high level scripting language like Ruby, but I'm definitely glad I don't have to write a C compiler where functions can take arbitrary blocks of code. That's a surprisingly good setup because in Smalltalk(one of ruby's main ancestor languages) if/else is a method which takes a block closure. a ifTrue: [ l log: 'a is true'] ifFalse: [ 'a is false'] while in ruby if else is a syntatic construct if a l.log ('a is true') else l.log ('a is false') end probably more for perceived clarity/comfortability then speeds sake.
- breadbox 13y agoWhich is exactly why you don't do it in a language where it isn't.
- kybernetikos 13y agoWhy should a developer have to have headspace for a special class of construct? And what difference does it make in practice whether it is a keyword or a function - why do you think it's so important that they are obviously different in the code - does their being part of a special category have any practical implications?
- deleted 13y ago[deleted]