Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:11:44+00:00 2026-05-23T13:11:44+00:00

The STL functors are implemented like this: template<class T> struct less{ bool operator()(T const&

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:11:45+00:00Added an answer on May 23, 2026 at 1:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The STL commonly defines an output iterator like so: template<class Cont> class insert_iterator :
Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter
I've implementation of UnaryOperation like this struct Converter { Converter( std::size_t value ): value_(
I have the following functor: class ComparatorClass { public: bool operator () (SimulatedDiskFile *
I am new to STL. I have written a Template Base class as follows
I enjoy developing algorithms using the STL, however, I have this recurring problem where
Studing STL I have written a a simple program to test functors and modifiers.
I have an stl unordered_map and I would like to store references to elements
I have a stl set of integers and I would like to iterate through
I am very new to the C++ STL, so this may be trivial. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.