Topic: iterator default constructor


Author: n2xssvv.g02gfr12930@ntlworld.com (n2xssvv g02gfr12930)
Date: Mon, 29 May 2006 18:55:46 GMT
Raw View
I can't find anything in the standard that states that the default
constructor for an iterator will ensure it's invalid. This would seem to
be the common sense thing to do, so has the standard or myself missed
out on some detail?

JB

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jdennett@cox.net (James Dennett)
Date: Mon, 29 May 2006 21:57:53 GMT
Raw View
n2xssvv g02gfr12930 wrote:
> I can't find anything in the standard that states that the default
> constructor for an iterator will ensure it's invalid. This would seem to
> be the common sense thing to do, so has the standard or myself missed
> out on some detail?

"Invalid" is not a runtime-detectable property.  If there
is nothing to say that an iterator is valid, it's invalid.
Invalid just means that you can't use its value.

To take one common case, if the iterator for a std::vector
or std::basic_string happens to be a raw pointer, then
(while it doesn't have a constructor), you can get an
invalid iterator with
   { std::string::iterator i; }
because that won't be initialized, and you can get an
invalid iterator with
   { std::string::iterator i = std::string::iterator(); }
where the latter is a valid pointer (specifically, it will
be a null pointer) but still an invalid iterator.

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "John Hickin" <hickin@nortelnetworks.com>
Date: Mon, 29 May 2006 17:55:12 CST
Raw View
"n2xssvv g02gfr12930" <n2xssvv.g02gfr12930@ntlworld.com> wrote in message
news:hVyeg.7318$sX1.4680@newsfe1-gui.ntli.net...
> I can't find anything in the standard that states that the default
> constructor for an iterator will ensure it's invalid. This would seem to
> be the common sense thing to do, so has the standard or myself missed
> out on some detail?

It is covered in 21.4 Iterator requirements /5. Basically an iterator that
is not associated with any container has a _singular value_. By singular
value I think you can think in terms of random value. It is not
dereferencable.


Regards, John.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Aaron Graham" <atgraham@gmail.com>
Date: Mon, 29 May 2006 21:47:26 CST
Raw View
James wrote:
> ... you can get an
> invalid iterator with
>    { std::string::iterator i = std::string::iterator(); }
> where the latter is a valid pointer (specifically, it will
> be a null pointer) but still an invalid iterator.

I've been aware of this fact for awhile now, but could you please tell
me where in the standard I can find the wording that specifically
describes this situation (explains why the pointer will be null)?  I
haven't been able to find it.

Thanks!

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Abrahams <dave@boost-consulting.com>
Date: Tue, 30 May 2006 08:48:40 CST
Raw View
"Aaron Graham" <atgraham@gmail.com> writes:

> James wrote:
>> ... you can get an
>> invalid iterator with
>>    { std::string::iterator i = std::string::iterator(); }
>> where the latter is a valid pointer (specifically, it will
>> be a null pointer) but still an invalid iterator.
>
> I've been aware of this fact for awhile now, but could you please tell
> me where in the standard I can find the wording that specifically
> describes this situation (explains why the pointer will be null)?  I
> haven't been able to find it.

I don't think you'll find that wording in the standard, because
there's no requirement that begin() be 0.  In fact, I'd be surprised
if you find any implmenetation that works that way: it's common for
implementations to always maintain a NUL terminator in the string and
use the same buffer for the string's data as for the result of c_str.
That would make it very inconvenient and inefficient for begin() to
ever return 0.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: n2xssvv.g02gfr12930@ntlworld.com (n2xssvv g02gfr12930)
Date: Tue, 30 May 2006 14:47:38 GMT
Raw View
John Hickin wrote:
> "n2xssvv g02gfr12930" <n2xssvv.g02gfr12930@ntlworld.com> wrote in message
> news:hVyeg.7318$sX1.4680@newsfe1-gui.ntli.net...
>
>>I can't find anything in the standard that states that the default
>>constructor for an iterator will ensure it's invalid. This would seem to
>>be the common sense thing to do, so has the standard or myself missed
>>out on some detail?
>
>
> It is covered in 21.4 Iterator requirements /5. Basically an iterator that
> is not associated with any container has a _singular value_. By singular
> value I think you can think in terms of random value. It is not
> dereferencable.
>
>
> Regards, John.
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]
>
I read that, but why not default construct the iterator to the
equivalent of a NULL pointer. This is what is done in gcc 3.3 and
probably a lot of other STL implementations, but as you've pointed out
the standard as no requirement for this. At least with a NULL pointer
you're going to get an exception if you try to dereference it, which is
why I generally reset them to NULL when not in use.

JB

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jdennett@cox.net (James Dennett)
Date: Tue, 30 May 2006 15:20:08 GMT
Raw View
David Abrahams wrote:
> "Aaron Graham" <atgraham@gmail.com> writes:
>
>> James wrote:
>>> ... you can get an
>>> invalid iterator with
>>>    { std::string::iterator i = std::string::iterator(); }
>>> where the latter is a valid pointer (specifically, it will
>>> be a null pointer) but still an invalid iterator.
>> I've been aware of this fact for awhile now, but could you please tell
>> me where in the standard I can find the wording that specifically
>> describes this situation (explains why the pointer will be null)?  I
>> haven't been able to find it.
>
> I don't think you'll find that wording in the standard, because
> there's no requirement that begin() be 0.

But we're talking about a value-initialized pointer,
not about begin() iterators.

> In fact, I'd be surprised
> if you find any implementation that works that way: it's common for
> implementations to always maintain a NUL terminator in the string and
> use the same buffer for the string's data as for the result of c_str.
> That would make it very inconvenient and inefficient for begin() to
> ever return 0.

The code above zero-initializes a pointer (as it's in the
context of an implementation where the iterator type is a
typedef for a raw pointer), and hence does give a null
pointer.  It's just
{
   typedef char * pointer;
   pointer p = pointer();
}
where the typedef happens (non-portably) to have the name
std::string::iterator.

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: hickin@nortelnetworks.com ("John Hickin")
Date: Tue, 30 May 2006 19:49:36 GMT
Raw View
"n2xssvv g02gfr12930" <n2xssvv.g02gfr12930@ntlworld.com> wrote in message
news:C6Yeg.8178$sX1.4462@newsfe1-gui.ntli.net...
      ]
> >
> I read that, but why not default construct the iterator to the
> equivalent of a NULL pointer. This is what is done in gcc 3.3 and
> probably a lot of other STL implementations, but as you've pointed out
> the standard as no requirement for this. At least with a NULL pointer
> you're going to get an exception if you try to dereference it, which is
> why I generally reset them to NULL when not in use.
>
> JB

It may be that T* can be an iterator and T* x gives x a random (i.e.,
singular) value, which in most cases is not NULL.

Regards, John.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]