Topic: (const) iterator question
Author: "James Kanze" <james.kanze@gmail.com>
Date: Wed, 21 Mar 2007 10:08:02 CST Raw View
On Mar 20, 8:46 pm, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
> 2b|!2b==? ha scritto:
> > If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
> First of all, you can assign NULL to a pointer, but you can't assign
> int(NULL) to a pointer.
I dont think that that was what he was saying (although you can
assign "int(NULL)" to a pointer, since it is a constant integral
expression evaluating to 0). I think that what he was saying
concerned int, with NULL just being an example.
And of course, you can't assign just any int to a pointer: it
has to be a constant integral expression evaluating to 0.
[...]
> > bool myParser::spellCheck(TokenList tokenList) {
> > TokenIterator tokenIterator = tokenList.begin();
> > TokenIterator lastSignificantToken = NULL; // <- complier barfs here
> Just use
> TokenIterator lastSignificantToken = TokenIterator();
Illegal. Undefined behavior, which means that it might work,
some of the time. What he wants is:
TokenIterator lastSignificantToken = tokenList.end() ;
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ 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: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 22 Mar 2007 02:45:09 GMT Raw View
James Kanze ha scritto:
>
>> Just use
>
>> TokenIterator lastSignificantToken = TokenIterator();
>
> Illegal. Undefined behavior, which means that it might work,
> some of the time. What he wants is:
Ouch! What a terrible mistake. Thanks for correcting me. Of course, what
I was thinking was to value-initialize the iterator, but we all know the
declarator syntax is not helping us... Maybe this one is better:
boost::value_initialize<TokenIterator> lastSignificantToken;
although this approach might add clutter to the code...
> some of the time. What he wants is:
> TokenIterator lastSignificantToken = tokenList.end() ;
>
I thought he wanted lastSignificantToken to be initialized with a
singular value (in case there is one), but tokenList.end() is valid
past-the-end iterator. Of course it might do, but it's not exactly the same.
Ganesh
---
[ 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: "James Kanze" <james.kanze@gmail.com>
Date: Thu, 22 Mar 2007 10:13:55 CST Raw View
On Mar 22, 3:45 am, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
> James Kanze ha scritto:
> > some of the time. What he wants is:
> > TokenIterator lastSignificantToken = tokenList.end() ;
> I thought he wanted lastSignificantToken to be initialized with a
> singular value (in case there is one), but tokenList.end() is valid
> past-the-end iterator. Of course it might do, but it's not exactly the same.
I know, and I'll admit that I'm really just guessing about what
he needs, from the name of the variable. I probably should have
been more explicit, but the names he used suggested that his
code was a misunderstood attempt at the two iterator idiom.
The STL idiom turns very heavily around the concept of the end
iterator. Just replacing a NULL pointer with the end iterator
won't, in general, work, but re-writing the code a bit to
conform to the STL conventions probably will. And when one has
access to the container (or an end iterator), container.end()
can almost always serve as a sentinal value as well. Again,
guessing at what he wanted, he will do something like:
while ( tokenIterator != tokenList.end() ) }
if ( isSignificantToken( *tokenIterator ) ) {
lastSignificantToken = tokenIterator ;
}
++ tokenIterator ;
}
If he finds no significant tokens, lastSignificantToken will
still be set to tokenList.end(), which can then be tested as a
sentinal value. (I agree that it's not as intuitive as a
special value, like NULL, but it is the STL convention.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ 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: root@your.box.com ("2b|!2b==?")
Date: Tue, 20 Mar 2007 15:11:35 GMT Raw View
If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
This works fine in debug, but fails to compile with Release
configuration ..:
bool myParser::spellCheck(TokenList tokenList) {
TokenIterator tokenIterator = tokenList.begin();
TokenIterator lastSignificantToken = NULL; // <- complier barfs here
..
Notes:
TokenIterator is typedefed as a const_iterator to a vector of tokens ...
---
[ 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: James Dennett <jdennett@acm.org>
Date: Tue, 20 Mar 2007 12:05:51 CST Raw View
2b|!2b==? wrote:
> If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
>
> This works fine in debug, but fails to compile with Release
> configuration ..:
>
>
> bool myParser::spellCheck(TokenList tokenList) {
> TokenIterator tokenIterator = tokenList.begin();
> TokenIterator lastSignificantToken = NULL; // <- complier barfs here
> ..
>
> Notes:
> TokenIterator is typedefed as a const_iterator to a vector of tokens ...
This isn't really topical here, but: if cases where the
iterator *is* a pointer, you *can* assign NULL to it
(though not an int, in general, only a null pointer
constant).
In most cases iterators are *not* pointers and will
not accept assignment of a "null" value. Iterators
are much more general than pointers; pointers are
one (somewhat interesting) special case of iterators.
-- 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: pete@versatilecoding.com (Pete Becker)
Date: Tue, 20 Mar 2007 17:04:04 GMT Raw View
2b|!2b==? wrote:
> If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
>
An iterator is not a pointer.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
---
[ 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: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
Date: Tue, 20 Mar 2007 12:05:50 CST Raw View
On Mar 20, 4:11 pm, r...@your.box.com ("2b|!2b==?") wrote:
> If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
I don't know whether your iterator is a pointer, perhaps yes,
perhaps not. Iterator's are generalized pointers and every
pointer fulfills all requirements of an iterator, but not
vice versa.
> This works fine in debug, but fails to compile with Release
> configuration ..:
>
> bool myParser::spellCheck(TokenList tokenList) {
> TokenIterator tokenIterator = tokenList.begin();
> TokenIterator lastSignificantToken = NULL; // <- complier barfs here
> ..
>
> Notes:
> TokenIterator is typedefed as a const_iterator to a vector of tokens ...
Your code does assume that the statement
TokenIterator lastSignificantToken = NULL;
is valid. Generally it is not so, because you don't know
what TokenList::const_iterator actually is and you should
not know that. It's quite unusual that under debug conditions
the assignement where well-defined, but that is all what
I can say without knowing the concrete standard library
implementation.
I propose that you use boost::optional<TokenIterator>
or simply a std::pair<TokenIterator, bool> where the bool
value signales whether first has already been set.
Greetings from Bremen,
Daniel Kr gler
---
[ 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: kuyper@wizard.net
Date: Tue, 20 Mar 2007 12:05:49 CST Raw View
"2b|!2b==?" wrote:
> If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
If an iterator is a pointer, then you can assign NULL to it. Are you
sure your particular iterator type is a pointer type? Many containers,
perhaps most, have iterators that are not pointers.
> This works fine in debug, but fails to compile with Release
> configuration ..:
>
>
> bool myParser::spellCheck(TokenList tokenList) {
> TokenIterator tokenIterator = tokenList.begin();
> TokenIterator lastSignificantToken = NULL; // <- complier barfs here
> ..
>
> Notes:
> TokenIterator is typedefed as a const_iterator to a vector of tokens ...
The key point, is whether or not const_iterator is a typedef for a
pointer type. Since your compiler complains, it's probably not.
---
[ 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: clarkcox3@gmail.com (Clark Cox)
Date: Tue, 20 Mar 2007 17:53:39 GMT Raw View
On 03/20/07, root@your.box.com ("2b|!2b==?") said:
> If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
Because iterators are not always pointers.
--
Clark S. Cox III
clarkcox3@gmail.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: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Tue, 20 Mar 2007 19:46:54 GMT Raw View
2b|!2b==? ha scritto:
> If an iterator is a pointer, then why can't I assign an 'int' (NULL) to it?
First of all, you can assign NULL to a pointer, but you can't assign
int(NULL) to a pointer. Second, you should never assume that an iterator
is actually a pointer, in fact most of the times iterators are not
pointers even when you expect them to be so (especially in debug builds).
>
> This works fine in debug, but fails to compile with Release
> configuration ..:
Are you sure? I was expecting it to be the other way round...
>
> bool myParser::spellCheck(TokenList tokenList) {
> TokenIterator tokenIterator = tokenList.begin();
> TokenIterator lastSignificantToken = NULL; // <- complier barfs here
Just use
TokenIterator lastSignificantToken = TokenIterator();
HTH,
Ganesh
---
[ 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 ]