Reputation: 71555
This came up at work today, so I thought I would throw it out to the community.
A co-worker wrote the following code (more or less):
#include <algorithm>
double chop(double x) {
return std::max(0, x);
}
But this does not even compile, because std::max
wants both of its arguments to have the exact same type. I believe this is so it can take a pair of references and return a reference, which is very likely what you want if you invoke it on a user-defined type. Fair enough.
The fix, of course, is to use std::max(0.0, x)
.
Now bear with me for a moment, because I lied. What my coworker actually wrote was this:
// Included from a very old header file written by someone long gone
template<class T1, class T2>
inline T1 myMax(T1 x, T2 y) {
return (x < y) ? y : x;
}
double chop(double x) {
return myMax(0, x);
}
This compiles! But it produces rather surprising results for x equal to, say, 0.25. I am not sure how long it took him to find the problem, and even after finding it, he had to ask why it was not working.
My answer was (a) Use 0.0 instead of 0 (which fixes the bug), and (b) use std::max
instead of myMax
(whose behavior is quite frightening when you think about it).
But he is wondering why that has to be. I mean, he can write 0 + x
or 0 * x
or 0 - x
, so why not myMax(0, x)
?
Here is my first pass at giving him what he wants:
// this is from the .hh file
// template meta-program to compute the "wider" of two types given as argument
template<class T1, class T2>
struct WiderType {
};
// Partial specialization for case where both types are same
template<class T>
struct WiderType<T, T> {
typedef T type;
};
// Specialization for first type "int" and second type "double"
template<>
struct WiderType<int, double> {
typedef double type;
};
template<class T1, class T2>
inline typename WiderType<T1,T2>::type
myMax(T1 a, T2 b) {
return ((a < b) ? b : a);
}
// this is from the .cc file
double chop(double x) {
return myMax(0, x);
}
// just to show this still works
int chop(int x) {
return myMax(0, x);
}
Now, I could go through and add a specialization for WiderType
for every pair of integral types, plus some to do the other Usual Arithmetic Conversions. (And I guess I could rename it UsualConversions
or somesuch.)
But is there an easier way? That is, does the C++ language give me a simple way to define my own function that performs the same conversions on its arguments as the various built-in arithmetic operators?
Upvotes: 2
Views: 143
Reputation: 75150
In addition to Charles Bailey's answer, you can do this as well:
template<typename T1, typename T2>
typename std::common_type<T1, T2>::type max(T1&& a, T2&& b) {
return a < b ? b : a;
}
common_type
has a typedef type
in it that is the type that both types can be implicitly converted to, so if it were, for instance, double
and int
, it would return a double
, but if it were int
and int
, it would return an int
.
If you can't use C++11 at all, then the only thing I can think of is this:
template<typename T1, typename T2, typename T3>
void max(const T1& a, const T2& b, T3& dst) {
dst = a < b ? b : a;
}
and use it like
double d;
max(0, 43.12, d);
It's pretty clumsy having to declare a variable that way though. If you think it's prettier, you can also do this:
template<typename RetType, typename T1, typename T2>
RetType max(const T1& a, const T2& b) {
return a < b ? b : a;
}
and then
return max<double>(0, 43.11);
Upvotes: 7
Reputation: 792547
I don't know of a good way before C++11 but now you can do something like this.
template<class T, class U>
auto myMax(T&& t, U&& u) -> decltype(t + u)
{
return t < u ? u : t;
}
decltype(t + u)
just works out what the common type for T
and U
would be in an arithmetic expression and uses that as the return type for the template.
Upvotes: 4