4 ms·
Macros really aren't about performance, though (or at least they shouldn't be; inlining and/or constexpr are a better solution to that problem in basically ever
by jeorgun 12y ago
Macros really aren't about performance, though (or at least they shouldn't be; inlining and/or constexpr are a better solution to that problem in basically every respect). They're either about simple, potentially extremely unsafe, syntax extensions (as in the article) or about using metadata that wouldn't otherwise be available (e.g. filenames/line numbers in the assert macro).
Not that I think macros are a good thing– 99% of the time they're best avoided– but dismissing them because 'performance isn't important' is basically a non sequitur.
- copsarebastards 12y agoPlenty of languages provide filenames/line numbers in assertions without using macros. Furthermore, `__LINE__` and `__FILE__` style macros are a pretty safe subset of macros that aren't really representative of the problems with macros. Compare: #include <stdlib.h> #include <stdio.h> #include "my_macro_library.h" int main() { int x = 0; printf("%i", increment(x)); printf("%i", __LINE__); return EXIT_SUCCESS; } There are a ton of different implementations of an `increment` macro which are going to cause undefined behavior here. Contrast with the `__LINE__` macro: it's implemented by the compiler and has unsurprising, well-defined behavior.