Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why is there no cfind() in the std containers?

Why is there no cfind() in the std containers?

Scheduled Pinned Locked Moved C / C++ / MFC
dockerquestionannouncement
3 Posts 2 Posters 0 Views 1 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.
  • C Offline
    C Offline
    Craig Longman
    wrote on last edited by
    #1

    I know that the cbegin()/cend() and such methods were implemented to allow for easy const type inference, but I'm curious as to why there wasn't any equivalent added for find()? Also, there isn't a real easy way to "trick" auto into inferring a const version, is there? Although it doesn't show itself as non-mutating in code, all I can think to do is use a const ref/pointer to the container, which would "force" a const_iterator return, right? However, I really like how cbegin() clearly states it's const-ness, right where it's being used. I've searched around off and on, looked through the (what I think is) the original proposal paper, nothing seems to hint as to why poor find() got the const-shaft. Curious, CraigL

    S 1 Reply Last reply
    0
    • C Craig Longman

      I know that the cbegin()/cend() and such methods were implemented to allow for easy const type inference, but I'm curious as to why there wasn't any equivalent added for find()? Also, there isn't a real easy way to "trick" auto into inferring a const version, is there? Although it doesn't show itself as non-mutating in code, all I can think to do is use a const ref/pointer to the container, which would "force" a const_iterator return, right? However, I really like how cbegin() clearly states it's const-ness, right where it's being used. I've searched around off and on, looked through the (what I think is) the original proposal paper, nothing seems to hint as to why poor find() got the const-shaft. Curious, CraigL

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #2

      The problem with find is that it's not a member function, and the const-ness of its return type depends on the const-ness of its arguments. If you call find with a search range defined by const_iterator's, the return type will be a const_iterator, whether you like it or not. Similarly, if you tried to define a cfind() function that should return a const_iterator even if the arguments defining the range are non-const iterator, then you're facing the problem of how to derive the former type from the latter - it's impossible! For begin and end it's different: their return types are derived from the list object, and it's easy to derive both iterator and const_iterator from the list type. find() has no such base type to refer to.

      C 1 Reply Last reply
      0
      • S Stefan_Lang

        The problem with find is that it's not a member function, and the const-ness of its return type depends on the const-ness of its arguments. If you call find with a search range defined by const_iterator's, the return type will be a const_iterator, whether you like it or not. Similarly, if you tried to define a cfind() function that should return a const_iterator even if the arguments defining the range are non-const iterator, then you're facing the problem of how to derive the former type from the latter - it's impossible! For begin and end it's different: their return types are derived from the list object, and it's easy to derive both iterator and const_iterator from the list type. find() has no such base type to refer to.

        C Offline
        C Offline
        Craig Longman
        wrote on last edited by
        #3

        Sorry, I should have been more specific. Rather than general containers (some of which, you're right, have to use the find function) the map/set and the new unordered ones. They do have a member function, so could quite easily do the same thing as cbegin()/cend(), at least I can't see any reason why not. For the not-to-const, I don't exactly follow. Gonna go play a bit to see what you mean. Thanks, CraigL

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups