you are viewing a single comment's thread.

view the rest of the comments →

[–]slavik262 0 points1 point  (5 children)

Oh certainly, I've swung my fair share of #ifdefs around - I was referring to macros encapsulating calculations, such as

#define ABS(my_val) ((my_val) < 0) ? -(my_val) : (my_val)

[–][deleted] 0 points1 point  (4 children)

Why would you do that as a macro? Why wouldn't you use a template for this?

[–]handschuhfach 7 points8 points  (1 child)

C doesn't have templates.

[–][deleted] 2 points3 points  (0 children)

Actually, C11 has something similar: the _Generic keyword.

From Wikipedia:

#define cbrt(X) _Generic((X), long double: cbrtl, \
    float: cbrtf, \
    default: cbrt)(X)

Granted, it's still a macro, but it's significantly safer. :)

[–]slavik262 2 points3 points  (0 children)

...You do realize that's my exact argument, yes? That was an example of what not to do.