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

倾然丶 夕夏残阳落幕 提交于 2019-12-23 19:38:58

问题


Does the C++ standard guarantee that the call

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

evaluates the functions f and g only once?


回答1:


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)


来源:https://stackoverflow.com/questions/3665574/only-one-evaluation-guaranteed-for-stdmin-stdmax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!