Evil preprocessor hack of the day [entries|reading|network|archive]
simont

[ userinfo | dreamwidth userinfo ]
[ archive | journal archive ]

Wed 2007-04-18 17:39
Evil preprocessor hack of the day
LinkReply
[identity profile] ewx.livejournal.comWed 2007-04-18 16:59

What's wrong with:

#define since(x) if(assert(x), 1)

(assert is required to expand to a void expression.)

Link Reply to this | Thread
[personal profile] simontWed 2007-04-18 17:25
Sensible standards requirements spoil all my fun ("you insensitive clod"). My technique, however, is extensible to other similar situations in which that guarantee isn't present :-P
Link Reply to this | Parent
[personal profile] fanfWed 2007-04-18 17:30
*snap*
Link Reply to this | Parent
[identity profile] tackline.livejournal.comWed 2007-04-18 17:57
I was thinking (and I've not been a C++ programmer for almost a decade(:

#ifdef NDEBUG
#define since(cond) if (0) ; else
#else
#define since(cond) if (!(cond)) { assert(0); } else
#endif

Link Reply to this | Parent
[personal profile] gerald_duckWed 2007-04-18 20:28
Why does it matter what assert() returns?

As a side note, since() plays merry hell with emacs's formatting…
Link Reply to this | Parent | Thread
[identity profile] gareth-rees.livejournal.comWed 2007-04-18 21:18
This could be fixed by hacking c-block-stmt-2-kwds in cc-langs.el, I think. But definitely a point against, yes.
Link Reply to this | Parent | Thread
[identity profile] ewx.livejournal.comWed 2007-04-18 21:27
It's sufficiently simple that you might just type it out rather than using a macro anyway.
Link Reply to this | Parent
[identity profile] ewx.livejournal.comWed 2007-04-18 21:25
The important point is that it expands to an expression, rather than (for instance) an if statement.
Link Reply to this | Parent
[identity profile] mooism.livejournal.comThu 2007-04-19 08:36
We want
if (foo)  handle_foo();
else if (bar)  handle_bar();
else since (baz)  handle_baz();
else since (quuz)  handle_quux();
to give a compile error.
Link Reply to this | Parent | Thread
[identity profile] mooism.livejournal.comThu 2007-04-19 08:38
Better example if the second since is an if.
Link Reply to this | Parent | Thread
[identity profile] womble2.livejournal.comThu 2007-04-19 22:29
gcc -Wunreachable-code -Werror
Link Reply to this | Parent | Thread
[identity profile] ewx.livejournal.comFri 2007-04-20 12:45
Or perhaps: #define since(x) if(assert(x),0);else (though I've not thought about this very hard yet).
Link Reply to this | Parent
navigation
[ go | Previous Entry | Next Entry ]
[ add | to Memories ]