仅保证对 std::min/std::max 进行一次评估 [英] Only one evaluation guaranteed for std::min/std::max

查看:30
本文介绍了仅保证对 std::min/std::max 进行一次评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 标准是否保证调用

Does the C++ standard guarantee that the call

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

只对函数 f 和 g 求值一次?

evaluates the functions f and g only once?

推荐答案

是的.由于 std::min 是一个函数, f(x) 和 g(x) 将只计算一次.并且不会复制返回的值.看函数原型:

Yes. Since std::min is a function, f(x) and g(x) will be evaluated only once. And returned values won't be copied. See the prototype of the function :

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

与预处理器真正定义的最小宏有明显区别:

It is a clear difference with preprocessor-genuinely-defined min macro :

#define MIN(A,B) ((A)<(B))?(A):(B)

这篇关于仅保证对 std::min/std::max 进行一次评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆