4 ms·
Not touching on the rest of what you said, but what's so bad about min? Apart from some internal clang stuff, like odd names and macros, it looks okay - I can s
by conistonwater 11y ago
Not touching on the rest of what you said, but what's so bad about min? Apart from some internal clang stuff, like odd names and macros, it looks okay - I can see what the function is doing.
// min
template <class _Tp, class _Compare>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const _Tp&
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
{
return __comp(__b, __a) ? __b : __a;
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
const _Tp&
min(const _Tp& __a, const _Tp& __b)
{
return _VSTD::min(__a, __b, __less<_Tp>());
}
#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
template<class _Tp, class _Compare>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp
min(initializer_list<_Tp> __t, _Compare __comp)
{
return *__min_element(__t.begin(), __t.end(), __comp);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp
min(initializer_list<_Tp> __t)
{
return *__min_element(__t.begin(), __t.end(), __less<_Tp>());
}
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
- Animats 11y agoIt used to be #define min(x,y) ((x) < (y) ? (x) : (y))
- pjmlp 11y agoWhich is anything but safe.
- azinman2 11y agoCan you explain more please?
- copascetic 11y agoBecause a macro in C/C++ is just text expansion, the given implementation will evaluate its arguments more than once, which will not be obvious at the "call" site and could have unintended consequences if the expressions have side-effects.
- azinman2 11y agoIs there another way to do it as a macro that avoids this? How do you have temporary variables potentially in the middle of a larger expression, e.g. if (min(a,b) == 3) {...}
- vbernat 11y agoBetter use: #define min(X, Y) \ ({ typeof (X) x_ = (X); \ typeof (Y) y_ = (Y); \ (x_ < y_) ? x_ : y_; })