Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::less a class template?

According to 20.8.5 §1, std::less is a class template with a member function:

template<typename T>
struct less
{
    bool operator()(const T& x, const T& y) const;
    // ...
};

Which means I have to mention the type when I instantiate the template, for example std::less<int>. Why isn't std::less a normal class with a member function template instead?

struct less
{
    template<typename T, typename U>
    bool operator()(const T& x, const U& y) const;
    // ...
};

Then I could simply pass std::less to an algorithm without the type argument, which can get hairy.

Is this just for historic reasons, because early compilers (supposedly) did not support member function templates very well (or maybe even at all), or is there something more profound to it?

like image 756
fredoverflow Avatar asked Dec 13 '12 20:12

fredoverflow


People also ask

What is std :: less?

The std::less is a is a member of the functional class (<functional. h>) used for performing comparisons. It is defined as a function object class for less than inequality comparison which returns a boolean value depending upon the condition. This can be used to change the functionality of the given function.

What is the difference between a class and a template in C++?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template.

Are C++ templates fast?

Templates are fully evaluated by the compiler, and so they have zero overhead at runtime. Calling Foo<int>() is exactly as efficient as calling FooInt() . So compared to approaches which rely on more work being done at runtime, for example by calling virtual functions, templates can indeed be faster.

Why are C++ templates useful?

Templates in C++ is an interesting feature that is used for generic programming and templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Simply put, you can create a single function or single class to work with different data types using templates.


1 Answers

It's so that the class created by the instantiated template has nested typedefs which provide type information about the result type and argument types of the functor:

  template <class Arg1, class Arg2, class Result>
  struct binary_function 
  {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
  };

  template <class T> 
  struct less : binary_function <T,T,bool> 
  {
    bool operator() (const T& x, const T& y) const;
  };

std::less inherits from std::binary_function, which produces these typedefs. So for example, you can extract the result type using std::less<T>::result_type.

Nowadays, this is mostly unnecessary with C++11's decltype and auto keywords.

like image 95
Charles Salvia Avatar answered Oct 19 '22 22:10

Charles Salvia