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 21 Mar 2022, 03:01 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!

    J 1 Reply Last reply 21 Mar 2022, 06:28
    0
    • V Vinoth Rajendran4
      21 Mar 2022, 14:50

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

      J Offline
      J Offline
      JonB
      wrote on 21 Mar 2022, 17:25 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 22 Mar 2022, 14:11
      4
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 21 Mar 2022, 03:43 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 21 Mar 2022, 04:32
        1
        • K Kent-Dorfman
          21 Mar 2022, 03:43

          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 21 Mar 2022, 04:32 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
            21 Mar 2022, 03:01

            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!

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 21 Mar 2022, 06:28 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 21 Mar 2022, 14:50
            2
            • J jsulm
              21 Mar 2022, 06:28

              @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 21 Mar 2022, 14:50 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.

              J 1 Reply Last reply 21 Mar 2022, 17:25
              0
              • V Vinoth Rajendran4
                21 Mar 2022, 14:50

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

                J Offline
                J Offline
                JonB
                wrote on 21 Mar 2022, 17:25 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 22 Mar 2022, 14:11
                4
                • J JonB
                  21 Mar 2022, 17:25

                  @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 22 Mar 2022, 14:11 last edited by
                  #7

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

                  1 Reply Last reply
                  0

                  1/7

                  21 Mar 2022, 03:01

                  • Login

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