News for hackers. Stuff that matters.
Re:Type-safe generic data structures in C by mgerdts on Friday April 09, 2021 @12:09AM (#26746609)
You mean:

#define SQ(x) ((x)*(x))

Re:Type-safe generic data structures in C by throwaway3neu94 on Saturday April 10, 2021 @02:24PM (#26763712)
There's another pitfall: it evaluates a parameter twice. Imagine calling SQ(func_with_side_effects()).

If you can assume a compiler with statement expressions and typeof (gcc, clang, probably icc in this case):

#define SQ(x) ({ typeof(x) x_ = (x); x_ * x_ })

If not (MSVC++) then best of luck.

Anyway I 100% agree people way, way overuse macros. Very often a static inline function is a better trade-off.

Re:Type-safe generic data structures in C by monocasa on Friday April 09, 2021 @12:34AM (#26746759)
Yeah, thanks. That's what I get for trying to type code on my phone.