5 ms·
Even modern C++ has eschewed macros for virtually every use case, the only reason I've had to use them (legitimately) has been conditional compilation and some
by qppo 6y ago
Even modern C++ has eschewed macros for virtually every use case, the only reason I've had to use them (legitimately) has been conditional compilation and some compiler/platform specific issues.
Like if you're doing #define PI 3.14159 you're asking to get chirped in code review.
- rightbyte 6y agoYe, everyone know it is: #define PI 3.1415927410125732421875 that is the float closest to pi or that there is the GNU extension USE_FUNKY_DEFINES-something with M_PI for cmath. (I know you meant constexpr:ions).
- Const-me 6y agoLong time ago when computers were 32-bit I used to write something like this in C++: inline double pi() { __asm fldpi; } On old enough compilers, this will get you 80-bit version of pi, more precision than even double. Completely useless now, modern compilers no longer emit x87 code.
- asveikau 6y agomath.h has M_PI in posix.
- qppo 6y agoThe point is that using macros for constants is frowned upon in modern codebases, just like all other use cases for macros that break type safety and the one definition rule.
- asveikau 6y agoYes, I understand the point. But to your comment in a literal sense, if I were doing code review I would probably suggest using M_PI. Which happens to be a macro. Which actually highlights that many things you want to use for practical purposes are C rather than C++, predate innovations such as const or namespaces, etc. It's possible and common to understand the downsides and still reach for the macro.