June 2025
Intermediate to advanced
1093 pages
33h 24m
English
And in another place, you might not get what you wanted at first glance. Compare the effects of the following two implementations.
// https://godbolt.org/z/jzKfabPh3#include <cmath> // sin, cosconstexpr double max2(double a, double b) { return a > b ? a : b; }#define MAX2(a,b) ((a) > (b) ? (a) : (b))int main() { double f = max2(sin(3.141592/2), cos(3.141592/2)); double e = MAX2(sin(3.141592/2), cos(3.141592/2));}
Listing 21.5 The purely textual substitution leads to multiple executions in macros.
For the max2 function, the arguments are evaluated before entering the function. The expensive sin() and cos() calculations therefore take place beforehand. In max2, only simple numerical values need to ...
Read now
Unlock full access