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. Other Discussions
  3. Clever Code
  4. subtle bug magnet --> std::vector's clear() and empty()

subtle bug magnet --> std::vector's clear() and empty()

Scheduled Pinned Locked Moved Clever Code
c++comgraphicsadobehelp
22 Posts 15 Posters 10 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.
  • W Warren Stevens

    Zac Howland wrote:

    You don't HAVE to put things in 2 files ... it is done out of convience for the most part. You CAN put everything for a class in a .h file if you really wanted to.

    Only in the most trivial cases. As soon as your project gets to a decent size, you do NOT want to touch a widely used .h file (or it takes forever to compile), which is why the split is needed. Also, if you have two classes that use functions of each other (which isn't that uncommon) the only way to do so is to use a forward declaration and two .h and .cpp files. :)


    www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

    Z Offline
    Z Offline
    Zac Howland
    wrote on last edited by
    #21

    Warren Stevens wrote:

    Only in the most trivial cases. As soon as your project gets to a decent size, you do NOT want to touch a widely used .h file (or it takes forever to compile), which is why the split is needed.

    Agreed. I said you could do it ... not that it was wise or desirable to do in all (or even most) cases.

    Warren Stevens wrote:

    Also, if you have two classes that use functions of each other (which isn't that uncommon) the only way to do so is to use a forward declaration and two .h and .cpp files.

    Ignoring the fact that if you have such, you may want to take a close look at your design to see why that is the case ... you CAN get away with having the code in just the .h files ... it just takes some extra work on some compilers.

    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

    1 Reply Last reply
    0
    • W Warren Stevens

      Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


      www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

      A Offline
      A Offline
      anti c
      wrote on last edited by
      #22

      I am sure a committee decided which names to use. I am not confused by these two names... This is not a std specific problem. We have the same problem in almost all libraries: empty, clear, remove, delete... hard choice. Luckily we cannot use void... we can use Void, _void, etc... :)

      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