Not logged in
Log in now
Create an account
Subscribe to LWN
Pencil, Pencil, and Pencil
Dividing the Linux desktop
LWN.net Weekly Edition for June 13, 2013
A report from pgCon 2013
Little things that matter in language design
Quotes of the week
Posted Apr 19, 2012 17:44 UTC (Thu) by BenHutchings (subscriber, #37955)
=> is_set___(, 1, 0)
=> is_set___(macrotest_BAR 1, 0)
Posted Apr 19, 2012 17:56 UTC (Thu) by wahern (subscriber, #37304)
__stringify uses the CPP # operator to convert a token into a string. It's almost always a pair of macros, because you need the indirection to allow the parameter (which may be composed of a CPP expression using the ## token pasting operator) to be replaced by any macro before applying the # operator. (Otherwise you could just the # without a macro invocation.) For example, this is very common:
#define STRINGIFY_(arg) #arg
#define STRINGIFY(arg) STRINGIFY_(arg)
Now, if you #define CONFIG_FOO 1, then __stringify(CONFIG_FOO) yields a string, "1". "1" yields '1'.
__stringify(CONFIG_BAR), where CONFIG_BAR isn't defined, yields a string, "CONFIG_BAR". That's because CONFIG_BAR isn't defined so remains as-is. "CONFIG_BAR" then yields 'C'.
There're lots of cool things you can do with the C pre-processor, including multi-dispatch based on the number of arguments using C99 variable-argument list macros and some cool macro magic--legal magic, mind you. (This also works in Visual Studio MSC because, although not even remotely C99 compliant, it at least does support __VA_ARGS__.)
With C11 you can use the new _Generic keyword to also implement multi-dispatch based on argument type, so in C11 you can implement full-blown function overloading in ISO C.
Posted Apr 19, 2012 19:35 UTC (Thu) by jzbiciak (✭ supporter ✭, #5246)
In any case, that trick is the cat's meow. Nice.
Posted Apr 19, 2012 20:33 UTC (Thu) by wahern (subscriber, #37304)
<sys/param.h> on Linux and BSD provide PASTE and XPASTE (the latter being more commonly used to ensure parameters are evaluated properly) to do this pasting in an ad hoc manner.
Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds