Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Using user defined type as key in std::map
Forum Updated to NodeBB v4.3 + New Features

Using user defined type as key in std::map

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 4 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    Hi All,

    When we use user defined type as key in std::map, we are forced to overload < operator, which I could understand the reason for it.
    But I could not understand the reason why the < operator overloading should be const function.

    Can some one please help me understand the reason why std::map is expecting the const function for < overloading ?

    Regards!

    jsulmJ 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      @jsulm : It make's perfect sense to make comparison operator constant. But like to know, where its defined that it's been expected.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #6

      @Vinoth-Rajendran4
      Then surely you just need to Google std::map?

      The first hit takes you to cppreference.com, which is thorough. On https://en.cppreference.com/w/cpp/container/map

      template<
          class Key,
          class T,
          class Compare = std::less<Key>,
          class Allocator = std::allocator<std::pair<const Key, T> >
      > class map;
      

      So click on std::less. https://en.cppreference.com/w/cpp/utility/functional/less

      bool operator()( const T& lhs, const T& rhs ) const;    (until C++14)
      constexpr bool operator()( const T& lhs, const T& rhs ) const;    (since C++14)
      

      So now you know it's const.

      V 1 Reply Last reply
      4
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #2

        because the boolean return value of the operator overload is expected to be const by the std::map template. C++ went bat-crap crazy with const a few years back, as if making EVERYTHING const somehow creates superior code. It's part of a larger problem, the belief among the younger crowd that the more code it takes to express an idea, the "better" it is. kind of like the mistaken assumption that 32-64 character identifiers are better. than single character loop control vars like (X,Y,Z,I,C)...

        V 1 Reply Last reply
        1
        • Kent-DorfmanK Kent-Dorfman

          because the boolean return value of the operator overload is expected to be const by the std::map template. C++ went bat-crap crazy with const a few years back, as if making EVERYTHING const somehow creates superior code. It's part of a larger problem, the belief among the younger crowd that the more code it takes to express an idea, the "better" it is. kind of like the mistaken assumption that 32-64 character identifiers are better. than single character loop control vars like (X,Y,Z,I,C)...

          V Offline
          V Offline
          Vinoth Rajendran4
          wrote on last edited by
          #3

          Thanks @Kent-Dorfman for the reply,

          because the boolean return value of the operator overload is expected to be const by the std::map template.

          Like to know, if there is any document where I could refer, on the return value expected to be constant ?

          1 Reply Last reply
          0
          • V Vinoth Rajendran4

            Hi All,

            When we use user defined type as key in std::map, we are forced to overload < operator, which I could understand the reason for it.
            But I could not understand the reason why the < operator overloading should be const function.

            Can some one please help me understand the reason why std::map is expecting the const function for < overloading ?

            Regards!

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Vinoth-Rajendran4 said in Using user defined type as key in std::map:

            But I could not understand the reason why the < operator overloading should be const function

            Why should it not?
            The comparision operator should not change the objects it compares (it would else introduce side effects which are hard to discover), so it is declared const.
            Also, const can help compiler to optimise code.

            https://en.cppreference.com/w/cpp/container/map/key_comp

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            V 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Vinoth-Rajendran4 said in Using user defined type as key in std::map:

              But I could not understand the reason why the < operator overloading should be const function

              Why should it not?
              The comparision operator should not change the objects it compares (it would else introduce side effects which are hard to discover), so it is declared const.
              Also, const can help compiler to optimise code.

              https://en.cppreference.com/w/cpp/container/map/key_comp

              V Offline
              V Offline
              Vinoth Rajendran4
              wrote on last edited by
              #5

              @jsulm : It make's perfect sense to make comparison operator constant. But like to know, where its defined that it's been expected.

              JonBJ 1 Reply Last reply
              0
              • V Vinoth Rajendran4

                @jsulm : It make's perfect sense to make comparison operator constant. But like to know, where its defined that it's been expected.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @Vinoth-Rajendran4
                Then surely you just need to Google std::map?

                The first hit takes you to cppreference.com, which is thorough. On https://en.cppreference.com/w/cpp/container/map

                template<
                    class Key,
                    class T,
                    class Compare = std::less<Key>,
                    class Allocator = std::allocator<std::pair<const Key, T> >
                > class map;
                

                So click on std::less. https://en.cppreference.com/w/cpp/utility/functional/less

                bool operator()( const T& lhs, const T& rhs ) const;    (until C++14)
                constexpr bool operator()( const T& lhs, const T& rhs ) const;    (since C++14)
                

                So now you know it's const.

                V 1 Reply Last reply
                4
                • JonBJ JonB

                  @Vinoth-Rajendran4
                  Then surely you just need to Google std::map?

                  The first hit takes you to cppreference.com, which is thorough. On https://en.cppreference.com/w/cpp/container/map

                  template<
                      class Key,
                      class T,
                      class Compare = std::less<Key>,
                      class Allocator = std::allocator<std::pair<const Key, T> >
                  > class map;
                  

                  So click on std::less. https://en.cppreference.com/w/cpp/utility/functional/less

                  bool operator()( const T& lhs, const T& rhs ) const;    (until C++14)
                  constexpr bool operator()( const T& lhs, const T& rhs ) const;    (since C++14)
                  

                  So now you know it's const.

                  V Offline
                  V Offline
                  Vinoth Rajendran4
                  wrote on last edited by
                  #7

                  @JonB : I am able to understand. Thanks for the detailed explanation.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved