Pete said:
That wasn't my intent.
Look again: every one of those approximately 100 specialiations is
needed. Every time you compare two objects of different types you need a
specialization. The default implementation only handles objects of the
same type. And that's the problem: this approach requires a
specialization for every pair of distinct types.
Not so. Get the Josuttis book on templates and read the chapter will he
covers exactly this case (Promotion_traits).
No, this approach requires a template specialization for every pair of
distinct types that the program compares.
That's not true.
I didn't say "for all new types". I said "for any additional types YOU
WANT TO SUPPORT." (emphasis added for the reading impaired)
Getting upset and yelling won't help.
I read what you wrote, and I responded to what you seemed to mean. I'm
sorry for any confusion on my part.
Anyway, it's still not true. As a naive first pass, a default template
to find the minimum of two values of different types might just choose
the larger type, as determined by sizeof. This won't always do the
right thing, but it's a reasonable default.
The original question, if you recall, was about handling pairs of
arguments of different types. This approach requires a template
specialization for every such pair. So, yes, it's "only" those types,
but they're the only ones that are interesting. Handling pairs of the
same type is simple.
Word games.
No, meaningful distinctions.
Adding stuff in order to make your template work is
maintenance, regardless of whether it's "sold separately."
Why do you think of it as "adding stuff?" Traits classes needing
specialization for a given type aren't just add-ons.
Not that it's
at all clear why someone would be selling these things: the question was
how to write a template that compares two objects of different types,
and the proposed solution consists of a long list of template
specializations. Who do you think is going to sell template
specializations for class MyType?
The designer or vendor of class MyType, if not someone else.
And how will they know that you need
to compare Mytype and YourType objects, so they can provide the
appropriate specializations?
They don't have to know that. At a minimum, they might specialize the
traits classes that are already part of the standard library (if
necessary), e.g. numeric_limits for a large integer type. If a vendor
also is providing a way to compare two values of a given type, it seems
reasonable that they might also specialize a common traits classes
related to comparisons, e.g. Promotion_traits. Of course, a client also
can request particular specializations, just as they might request any
other part of an interface explicitly.
I've said nothing about traits classes. What I said is that writing
N*(N-1) template specializations to handle all pairs of N argument types
isn't my idea of clean code.
That may be what you meant, but it's not what you said.
I prefer to let the compiler do that kind
of work -- it's much better at it.
Not so. The compiler does the right thing in many, but not all, cases.
(Note that the sample implementation
doesn't get the platform-dependent promotion semantics right, while the
compiler almost certainly does).
Then platform-dependent types should rely on the compiler to do the
right thing. The compiler won't always do the right thing, though, and
it should be possible for users to specialize the cases without changing
the user-visible interface.