22

The STL functors are implemented like this:

template<class T>
struct less{
  bool operator()(T const& lhs, T const& rhs){
    return lhs < rhs;
  }
};

This makes us mention the (possibly long) type everytime we create such a functor. Why are they not implemented like shown below? Any reasons?

struct less{
  template<class T>
  bool operator()(T const& lhs, T const& rhs){
    return lhs < rhs;
  }
};

That would make them usable without any mentioning of (possibly long) types.

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • as of C++14, you can use [transparant comparators](http://stackoverflow.com/q/20317413/819272) that provides `template std::less` with `std::less` containing the templated `operator<` (and similarly for the other functors). – TemplateRex Aug 03 '14 at 13:06

2 Answers2

23

It would also make it impossible to specialize them for user defined types.

They are supposed to be a customization point.


To summarize the discussions in the comments:

Although it is technically possible to do like Xeo suggests, the language standard doesn't allow it.

It is very hard to write a working class template if users are allowed to specialize individual functions of the template. In some cases it might however be a good idea to specialize the whole class for a user defined type.

Therefore the C++98 standard writes (17.4.3.1):

It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std.

As it isn't "otherwise specified" that Xeo's code is allowed, we are to understand that it is not. Perhaps not totally obvious! Or that "template specializations" only apply to classes.

The new C++11 standard has had this part expanded, and spells it out in more detail (17.6.4.2):

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

The behavior of a C++ program is undefined if it declares

— an explicit specialization of any member function of a standard library class template, or
— an explicit specialization of any member function template of a standard library class or class template, or
— an explicit or partial specialization of any member class template of a standard library class or class template.

A program may explicitly instantiate a template defined in the standard library only if the declaration depends on the name of a user-defined type and the instantiation meets the standard library requirements for the original template.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Well.. how about, make your own? You need to make a "new" struct anyways. – Xeo Jul 04 '11 at 21:11
  • Just in case what Bo said is difficult to understand, you might want to define template <> bool less::operator()(...) {... } – fulmicoton Jul 04 '11 at 21:11
  • @poule: `template<> bool std::less::operator()` ? Or am I missing something? – Xeo Jul 04 '11 at 21:14
  • 2
    @Xeo Then you’d always need to specify your custom functor whereas `less` can be inferred in most places. – Konrad Rudolph Jul 04 '11 at 21:14
  • Why would you want `less` to do something different than `operator<`? Seems like that would be really confusing. – interjay Jul 04 '11 at 21:15
  • Your own version wouldn't have the same name ;) For example, std::less is the default functor type to compare keys in an std::map, so if your key type needs a special type, you can customize std::less by implementing it for your specific type. You have other ways to solve this specific case, but the point is that "customization point" really means "keep the same name but use a specific tuned optimization for a given parameter type". – Klaim Jul 04 '11 at 21:15
  • Also, why not simply define a `bool operator<` for your user defined type? – Xeo Jul 04 '11 at 21:16
  • @Xeo - You might not want an operator< for some types, but still be able to put them in a set or a map. It's just an option you can choose. – Bo Persson Jul 04 '11 at 21:19
  • @Xeo : would that compile when some cpp include this less specialization and some doesn't? (that's really a question) – fulmicoton Jul 04 '11 at 21:20
  • @Bo: Okay, my other point about specializing the operator [still stands](http://ideone.com/zBaLn). – Xeo Jul 04 '11 at 21:20
  • @poule: Yes, it would and does, see my last comment. Looks a bit ugly tho'. – Xeo Jul 04 '11 at 21:20
  • @>Xeo - If the operator is a template, I cannot specialize it for `MyType`. I also cannot pass the operator as a template parameter, but with std::less it is possible. – Bo Persson Jul 04 '11 at 21:23
  • @Bo: Huh? I just did the specialization in the Ideone example! Also, you can just pass just `less` as the template parameter then. – Xeo Jul 04 '11 at 21:26
  • 2
    @Xeo - Please! Can not, as in **is not allowed**. You are **allowed** to specialize **types** from namespace std. – Bo Persson Jul 04 '11 at 21:29
  • @Bo: You are also allowed to specialize functions. – Xeo Jul 04 '11 at 21:30
  • @Xeo - No, you can only specialize class templates. C++98 says so in 17.4.3. C++11 explicitly says it's UB to specalize a member function (17.6.4.2). – Bo Persson Jul 04 '11 at 21:41
  • @Bo: Meh, bent the rules then. :< Oh, and one point you might want to add, since you got the only answer, a point that I overlooked, are the typedefs in `binary_function` from which `less` derives. The typedefs wouldn't be possible without a templated `struct`. – Xeo Jul 04 '11 at 21:43
  • @Xeo my bad it is compiling on gcc. I was pretty sure it was forbidden. – fulmicoton Jul 04 '11 at 21:43
  • @Xeo Funny thing, I get different result when doing separate compilation with optimization flags, and without optimization flags. I totally see how inlining is involved, but I wonder if it would be the same with the STL-fashion code. – fulmicoton Jul 04 '11 at 21:49
  • @BoPersson: Use "you _shall_ only specialize class templates" and "I _shall_ not specialize it for `MyType`". :) – Lightness Races in Orbit Jul 04 '11 at 22:03
5

Maybe:

std::multiset<long, std::less<int> > moduloset;

Odd thing to do, but the point is that std::less<int>, std::less<long>, std::less<unsigned int> implement different mathematical functions which produce different results when passed (the result of converting) certain argument expressions. Various algorithms and other standard library components work by specifying a functor, so it makes sense to me that there are different functors to represent those different mathematical functions, not just different overloads of operator() on one functor.

Furthermore, a functor with a template operator() can't be an Adaptable Binary Predicate, since it doesn't have argument types (an argument can have any type). So if std::less were defined as you suggest then it couldn't participate in the stuff in <functional>.

Also on a highly speculative note -- std::less was probably designed before support for template member functions was at all widespread, since there are various notes in the SGI STL documentation that say, "if your implementation doesn't support member templates then this isn't available". For such a simple component there would, I guess, be an incentive to do something that works today. Once it exists, the standardization could then have removed it in favour of something else, but was it worth disrupting existing code? If it was that big a deal, then either you or the standard could introduce a flexible_less functor as you describe.

Finally, why

template<class T>
bool operator()(T const& lhs, T const& rhs){
  return lhs < rhs;
}

rather than

template<class T, class U>
bool operator()(T const& lhs, U const& rhs){
  return lhs < rhs;
}

For user-defined types, the two might not be the same. Yes, this is an unfair question, since I don't know why there's no two-template-argument version of std::less ;-)

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • "since I don't know why there's no two-template-argument version of std::less" - Make that a new question! :P – Xeo Jul 05 '11 at 07:00