Only one estimate guaranteed for std :: min / std :: max

The C ++ standard ensures that the call

c = std::min(f(x), g(x));

evaluates the functions f and g only once?

+3
source share
1 answer

Yes. Since std :: min is a function, f (x) and g (x) will be evaluated only once. And the returned values ​​will not be copied. See Function Prototype:

template<typename T>     
const T& min ( const T& a, const T& b );

This is a clear distinction with a mini macro defined by a preprocessor:

#define MIN(A,B) ((A)<(B))?(A):(B)
+12
source

Source: https://habr.com/ru/post/1763741/


All Articles