Topic: Re:What should <STLContainer>::end() return?


Author: scherrey@proteus-tech.com (Benjamin Scherrey)
Date: 1995/06/06
Raw View
In message <3r0rgh$n2e@news.doit.wisc.edu> - khan@xraylith.wisc.edu (Mumit Khan
) writes:

:>Yes, it's documented in the ANSI draft. Take a look at secs. 23.1.4
:>[lib.containers.requirements] and 24.1.5 [lib.iterators.requirements].
:>The first one says that each container must support begin() and end()
:>and the corresponding meanings of what they are, and the second ref
:>gives a bit more info on past-the-end iterator value.

 Thanx for the pointer. I think I missed the second one.

:>
:>If end() were to return a default constructed object, how would you know
:>if you've reached the end? (I'm assuming you're checking to see if you've
:>reached the end of the container or some such thing) What if there are
:>other objects in the container that are not different than the default?

 I think the container/iterator should have an is_null() method. The
container should have one default constructed instance of the class which the
iterator would point to when it goes past the end. The is_null method should
compare the address of the null object to determine its result. The "null
object" would not actually be contained in the primitive container (list,
tree, what ever) so you would not iterate through it, nor would it conflict
with any items that might actually be default constructed instances. The main
benifit of this would be the ability to dereference the null object without
worrying about causing a memory fault.

//
// Benjamin Scherrey        Proteus Technologies, Inc.
// scherrey@proteus-tech.com       (404) 454-1013v 986-9876f
//






Author: scherrey@proteus-tech.com (Benjamin Scherrey)
Date: 1995/06/06
Raw View
In message <3qvp4u$19fe@news.doit.wisc.edu> - khan@xraylith.wisc.edu (Mumit
Khan) writes:
:>Remember that STL tries to emulate C pointer semantics (or at least as
:>faithfully as it possibly can), and end() returns something analogous
:>to a 0 pointer in C.

 This seems reasonable but I wish it would return a default constructed
object. This obviously isn't happening and I'm getting complete garbage.
Would be nice to have it return a "null" object, IMHO. Also, am I missing
something or is this fact not documented in the ANSI draft? I couldn't find
it (thus my previous message)!

:>looks like another item for STL.newbie guide:
:>
:>STL_Container::begin() -- returns the *first* item in the container if
:>    it exists or end() otherwise.
:>STL_Container::end() -- returns one-past the end of the container.

 Indeed!

//
// Benjamin Scherrey        Proteus Technologies, Inc.
// scherrey@proteus-tech.com       (404) 454-1013v 986-9876f
//