Topic: STL binary functions & default templates


Author: mcg@wheezy.CS.Berkeley.EDU (Michael C. Greenspon)
Date: 1996/11/24
Raw View
> You correctly point out that the standard function objects, like
> equal_to<T> force both arguments to be of the same type, even though
> that is not always what is wanted. I suspect the reason for this is that
> 90% of the time, both arguments are the same. Defining equal_to<T1,T2>
> would require users to type something like equal_to<int,int>, which
> could be annoying. I solved this problem by very easily creating a
> function object called het_equal_to<T1,T2> for comparing heterogeneous
> arguments. (You might also want het_less_than<>, het_greater_than<>,
> etc.)
>

Couldn't these be defined using default template args as something like--

template <class T, class U = T>
class equal_to : public BinaryFunction<bool, T, U> ...

so that equal_to<T> => equal_to<T,T> ? Thus the existing syntax is
preserved but we gain the capability to use the operator fns on two
different types for which there is a conversion defined.  If this is a
misuse of default template args then please fix the standard since this is
just one case where using a template arg as a default for another template
arg is very useful. Could some of the language implementor gurus comment
on this situation with the operator fns please?


Thanks,
--Michael
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]