views:

159

answers:

3

For any STL container that I'm using, if I declare an iterator (of this particular container type) using the iterator's default constructor, what will the iterator be initialised to?

For example, I have:

std::list<void*> address_list;
std::list<void*>::iterator iter;

What will iter be initialised to?

+5  A: 

The iterator is not initialized, just as int x; declares an integer which isn't initialized. It does not have a properly defined value.

JesperE
Is there a way to initialise iter to NULL?
The Void
@The Void: Your question makes no sense. `NULL` is a value _pointers_ may have, bot iterators. While all pointers are iterators, not all iterators are pointers.
sbi
So, while there is such a thing as a NULL pointer, there's no such thing as a "NULL iterator"?
The Void
@The Void: An iterator is an object, and there is no such thing as a "NULL object". (Yes, there is that pattern, but that's a completely different thing.) The closest thing to a "NULL iterator" is the singular value described in my answer.
FredOverflow
There is no such thing as an "empty" or "null" iterator. It's the same as a lock for instance. You can't talk about an "empty" or a "null" lock, this just makes no sense.
Tomaka17
@JesperE: It most probably is initialized (iterators in many cases are classes, and they will have a default constructor that initializes the contents).
David Rodríguez - dribeas
@sbi: is bot a new abbreviation for "but not"? :)
codymanix
@codymanix: Yeah, something like this. I guess thinking got ahead of typing and my fingers cheated to catch up. `:)`
sbi
+6  A: 

The default constructor initializes an iterator to a singular value:

Iterators can also have singular values that are not associated with any sequence. [ Example: After the declaration of an uninitialized pointer x (as with int* x;), x must always be assumed to have a singular value of a pointer. —end example ] Results of most expressions are undefined for singular values [24.2.1 §5]

FredOverflow
My standardese aversion striking again. `<sigh>` What does that mean in understandable speech?
sbi
@sbi: Well, the paragraph goes on and on, I decided to cut it. Basically, you are not allowed to do anything useful with a singular value, for example dereference it or compare it.
FredOverflow
@sbi: just replace all instances of "singular" with "weird". You're not allowed to do anything with it because it's in a weird state.
jalf
@jalf ` certainly wouldn't initialize `it` to a special value, while `std::vector<T>::iterator it = std::vector<T>::iterator();` would.
sbi
It might be useful to consider singular iterators "write-only, do not point to a sequence (yet)". As they're write-only, you can't talk about "the value they have".
MSalters
@sbi: For user-defined types T, the definition `T x;` **always** initializes x with the default constructor (if there is one, of course).
FredOverflow
@Fred: Yes, which is why I asked about built-in pointers.
sbi
sbi
@sbi: It is not required to have any special value, it is basically degenerate: you can't really do anything with it, other than assign to it to bring it into a well-defined non-singular state. An uninitialized pointer would be an example of a singular iterator.
jalf
@jalf: Thanks for clarifying that. I consider "singular value" a badly coined name for something that could have _any_ possible value. It sure threw me off....
sbi
+1  A: 

By convention a "NULL iterator" for containers, which is used to indicate no result, compares equal to the result of container.end().

 std::vector<X>::iterator iter = std::find(my_vec.begin(), my_vec.end(), x);
 if (iter == my_vec.end()) {
     //no result found; iter points to "nothing"
 }

However, since a default-constructed container iterator is not associated with any particular container, there is no good value it could take. Therefore it is just an uninitialized variable and the only legal operation to do with it is to assign a valid iterator to it.

 std::vector<X>::iterator iter;  //no particular value
 iter = some_vector.begin();  //iter is now usable

For other kinds of iterators this might not be true. E.g in case of istream_iterator, a default-constructed iterator represents (compares equal to) an istream_iterator which has reached the EOF of an input stream.

UncleBens