Specializing std::less without an operator <
Hello,
Suppose one has a class to represent objects for which a total
ordering can be defined, but where the exact nature of the ordering is
not always meaningful, and in general cannot be made non-arbitrary.
(In my case the class represents a physical measurement unit, e.g.
"meters" or "seconds"... this must be modifiable at runtime so (as
well as for historical reasons) we couldn't easily use Boost.Units.)
Suppose further that defining some (albeit arbitrary) total ordering
IS highly desirable: there is no reason one wouldn't want to have
these objects usable in an std::set, as the key of an std::map, or in
a container where one would want to std::sort() them by the
(arbitrary, but default) total ordering in order to be able to find
one via binary search.
I've read in _Effective STL_, item 42, "Make sure less<T> means
operator <". The arguments presented there are of course very
reasonable ... but I think I can make a reasonable case that in this
situation the advice of _Effective STL_ does not apply. Comments are
welcomed.
1) Providing an operator < () would be misleading, for the following
reasons. (For explicative purposes please presume the existence of a
constructor Unit::Unit(const char *) that does the right thing when
used as in the code fragments below.)
a) The philosophical perspective: The result of
operator < (Unit("feet"), Unit("meters"))
could perhaps meaningfully return true or false, as the length of 1
foot is less than 1 meter, but what would
operator < (Unit("seconds"), Unit("degrees Kelvin"))
return?? The only two possibilities that are reasonable are
i) It should throw (which means operator< is unusable in a container
or sort function, so back to square one), or
ii) It should return a well-defined but arbitrary result (misleading).
b) The pragmatic perspective: The representation of a Unit is a single
unsigned value, which is treated as a bitmask, but for historical
reasons the values in the bits have no relation to the magnitude of
the physical quantity they represent. So even to support the specific
desideratum that operator < () returns true when the LHS and RHS are
of the same physical dimension and LHS is "smaller" than RHS would
require an inefficient series of switch / case or if ... else if
statements.
2) However, given the function unsigned Unit::get() const, which
simply returns the wrapped unsigned value, writing an override of
std::less<Unit> to provide a well-defined (but in general arbitrary)
total ordering is trivial, as below. And since no one writes
"std::less<T>()(a, b)" when what they really mean is to write "a < b"
-- that is, std::less is seldom used explicitly aside from in the
context of needing a total ordering -- I feel it is not misleading.
#include <functional>
....
namespace std {
template <> struct less<Unit> : binary_function <Unit, Unit, bool>
{
bool operator() (const Unit & x, const Unit & y) const
{ return x.get() < y.get(); }
};
}
Should I nevertheless feel morally bound by the advice of _Effective
STL_, and require that all users of this class use their own functor
(even if a default one is provided), despite the excess verbiage?
// assume struct UnitCmp defined just as std::less<Unit> was above
std::set<Unit, UnitCmp> s_units;
std::vector<Unit> v_units;
// ...
std::sort(v_units.begin(), v_units.end(), UnitCmp());
auto it = std::lower_bound(v_units.begin(), v_units.end(),
Unit("meters"), UnitCmp());
Or, as boost::shared_ptr does, is it regarded as preferable to instead
go ahead and define an operator<() that returns lhs.get() < rhs.get()
even though the result of the operator< is in general meaningless?
Thanks in advance for any responses,
- Kevin B. McCarty
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]