3 ms·
This in cpp: template<class T> constexpr const T& max(const T& a, const T& b) { return a>b ? a : b; }
by andrepd 9y ago
This in cpp:
template<class T>
constexpr const T& max(const T& a, const T& b) {
return a>b ? a : b;
}
- mehrdadn 9y agoEdit: apologies, I'm blind; never mind. I thought I double-checked but the use of > instead of < tripped me up.
- darkmx0z 9y agoStepanov disagrees. You should return the second parameter if a, b are equivalent (and you should use < anyway): template<typename T> inline constexpr const T& max(const T& a, const T& b) { return (b < a ? a : b); }
- loeg 9y agoThe C version works with distinct types for a and b. Does this?
- Jyaif 9y agoThat's not a feature, it's more bug prone.
- andrepd 9y agoThis one works too, if they are implicitly convertible (e.g. max(1, 2.0))
- bstamour 9y agoNo it won't. You'll get a compile time error, as T could either deduce to int or double. Unless you explicitly specify it as max<int>(1, 2.0) or max<double>(1, 2.0).