3 ms·
One of my favorite C preprocessor hacks - emulating named parameters in C. https://gist.github.com/robmccoll/5236408528c8c664c201 https://gist.github.com/robmc
by robmccoll 12y ago
One of my favorite C preprocessor hacks - emulating named parameters in C.
https://gist.github.com/robmccoll/5236408528c8c664c201 https://gist.github.com/robmccoll/5236408528c8c664c201
- kentonv 12y agoIf only named member initializers were supported in C++. Here's a similar hack that works in C++11, though it unfortunately requires declaring a global variable for each name (which I make less-bad by prefixing them with $, which is non-standard but supported by all major compilers): https://gist.github.com/kentonv/7553792 https://gist.github.com/kentonv/7553792
- captaincrowbar 12y agoI did something similar (without macros, with a bit more type safety since the keywords have an associated type, and with a shortcut for boolean arguments): https://github.com/CaptainCrowbar/kwargs https://github.com/CaptainCrowbar/kwargs
- pb2au 12y agoPretty cool hack, but not one I'd hope to run across in any real code. Between the nondescript function header and the inability to differentiate between an unset argument and a zero-set one, this macro would too quickly become a headache.
- wahern 12y agoYou can pre-initialize any member you want to something other than 0, such as -1. The C standard permits multiple initializations of the same member, and defines the value to be that of the last initialization. So if the application sets the member again, it all works properly. Both GCC and clang complain about such initializer "overriding", but I habitually disable those warnings whenever I start a new project. (I also contributed the patch to clang to be able to disable that warning =) Initializer overrides and missing braces are the two dumbest diagnostics that, sadly, both GCC and clang spit out. Using '{ 0 }' is the _only_ way to initialize a compound automatic variable where you do not know (and shouldn't depend on) the internal layout, and it's perfectly legal C.