subtle bug magnet --> std::vector's clear() and empty()
-
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)
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
-
Bring me the head of the person who decided on the names
clear()
andempty()
forstd::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 writtenempty()
when I should have writtenclear()
(i.e. when I'm trying to get rid of the contents of the vector - but instead just return abool
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? usedMakeEmpty()
andIsEmpty()
or at leastEmpty()
andIsEmpty()
like the CString designers did. Rant over.:sigh:
www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)
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... :)