C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Iterator Question
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Bob Bell
Guest





PostPosted: Tue Oct 14, 2003 9:10 am    Post subject: Iterator Question Reply with quote



All-

Are default-constructed standard container iterators guaranteed to
compare equal? As in

#include <vector>

void F()
{
std::vector<int>::iterator a, b;

if (a == b) {

// ...

}
}

Does the standard say whether "// ..." will be executed or not?

Bob

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
David Abrahams
Guest





PostPosted: Tue Oct 14, 2003 8:55 pm    Post subject: Re: Iterator Question Reply with quote



[email]belvis (AT) pacbell (DOT) net[/email] (Bob Bell) writes:

Quote:
All-

Are default-constructed standard container iterators guaranteed to
compare equal? As in

Unfortunately, no

Quote:
#include <vector

void F()
{
std::vector
if (a == b) {

That's undefined behavior.

Quote:
// ...

}
}

Does the standard say whether "// ..." will be executed or not?

Comments are all executed at program startup time, but their effects
are, well, somewhat limited ;->

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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Tue Oct 14, 2003 9:08 pm    Post subject: Re: Iterator Question Reply with quote



Hi,

Bob Bell wrote:

Quote:
Are default-constructed standard container iterators guaranteed to
compare equal?

No. Such a rule would not be really useful - I mean it would not give
you much more than you could gain by following good coding guidelines
and not using uninitialized variables at all. Implementing this rule
would also be a cost that not everyone would be willing to pay.

Iterators are supposed to be implementable in as efficient way as
possible. On the extreme, vector<>::iterator can be a plain pointer, so:

Quote:
std::vector<int>::iterator a, b;

can be no different than:

int *a, *b;

Quote:
if (a == b) {

Now nobody knows what happens - it is not a simple true/false
instruction. On the extreme, it can be undefined behavior, resulting in
whatever you expect or anything you do not expect.

Just do not use uninitialized variables in your code.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Oct 14, 2003 9:15 pm    Post subject: Re: Iterator Question Reply with quote

On 14 Oct 2003 05:10:12 -0400, [email]belvis (AT) pacbell (DOT) net[/email] (Bob Bell) wrote:

Quote:
Are default-constructed standard container iterators guaranteed to
compare equal?

std::vector<int>::iterator a, b;

if (a == b) {

Identical to:
int* p; int* q;
if (p == q)

Just looking at an uninitialized iterator is undefined behavior. The
standard offers no protection for your hard drive in the above code.
See 24.1/5 and TC.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
johnchx
Guest





PostPosted: Tue Oct 14, 2003 9:19 pm    Post subject: Re: Iterator Question Reply with quote

[email]belvis (AT) pacbell (DOT) net[/email] (Bob Bell) wrote
Quote:

Are default-constructed standard container iterators guaranteed to
compare equal? As in


Is there a requirement that such iterators can be default-constructed at
all?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Joshua Lehrer
Guest





PostPosted: Wed Oct 15, 2003 10:44 am    Post subject: Re: Iterator Question Reply with quote

[email]belvis (AT) pacbell (DOT) net[/email] (Bob Bell) wrote in message news:<c87c1cfb.0310131409.59a88f4e (AT) posting (DOT) google.com>...
Quote:
All-

Are default-constructed standard container iterators guaranteed to
compare equal? As in

#include <vector

void F()
{
std::vector
if (a == b) {

// ...

}
}

Does the standard say whether "// ..." will be executed or not?


Replace "std::vector<int>::iterator" with "int*", which it is allowed
to be, and you get your answer:

char * a,b;
if (a==b) {
}

'a' and 'b' are uninitialized and therefore the comparison has
undefined results.

joshua lehrer
factset research systems
NYSE:FDS

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Wed Oct 15, 2003 10:45 am    Post subject: Re: Iterator Question Reply with quote

On 14 Oct 2003 17:19:45 -0400, [email]johnchx2 (AT) yahoo (DOT) com[/email] (johnchx) wrote:

Quote:
belvis (AT) pacbell (DOT) net (Bob Bell) wrote

Are default-constructed standard container iterators guaranteed to
compare equal? As in

Is there a requirement that such iterators can be default-constructed at
all?

Yes. All standard containers have at least forward iterators and
forward iterators must be default constructable. See table 74 and
note that they might be singular (undefined lvalue to rvalue
conversion).

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Joshua Lehrer
Guest





PostPosted: Wed Oct 15, 2003 12:52 pm    Post subject: Re: Iterator Question Reply with quote

[email]johnchx2 (AT) yahoo (DOT) com[/email] (johnchx) wrote in message news:<4fb4137d.0310140959.3a926535 (AT) posting (DOT) google.com>...
Quote:
belvis (AT) pacbell (DOT) net (Bob Bell) wrote

Are default-constructed standard container iterators guaranteed to
compare equal? As in


Is there a requirement that such iterators can be default-constructed at
all?


Yes. A Random Access Itertor is a refinement of Bidirectional
Iterator which is a refinement of Forward Iterator which is a
refinement of Input Iterator which is a refinement of Trivial Iterator
which is a refinement of Default Constructible.

http://www.sgi.com/tech/stl/RandomAccessIterator.html
http://www.sgi.com/tech/stl/BidirectionalIterator.html
http://www.sgi.com/tech/stl/ForwardIterator.html
http://www.sgi.com/tech/stl/InputIterator.html
http://www.sgi.com/tech/stl/trivial.html
http://www.sgi.com/tech/stl/DefaultConstructible.html

Thanks SGI.

joshua lehrer
factset research systems
NYSE:FDS

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michael Birck
Guest





PostPosted: Wed Oct 15, 2003 10:02 pm    Post subject: Re: Iterator Question Reply with quote

[email]johnchx2 (AT) yahoo (DOT) com[/email] (johnchx) wrote in news:4fb4137d.0310140959.3a926535
@posting.google.com:

Quote:
belvis (AT) pacbell (DOT) net (Bob Bell) wrote
Is there a requirement that such iterators can be default-constructed at
all?

Yes - otherwise you couldn't put iterators in a standard container.

/Michael

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Thu Oct 16, 2003 2:10 pm    Post subject: Re: Iterator Question Reply with quote

On 15 Oct 2003 08:52:27 -0400, [email]usenet_cpp (AT) lehrerfamily (DOT) com[/email] (Joshua
Lehrer) wrote:

Quote:
johnchx2 (AT) yahoo (DOT) com (johnchx) wrote in message news:<4fb4137d.0310140959.3a926535 (AT) posting (DOT) google.com>...
[email]belvis (AT) pacbell (DOT) net[/email] (Bob Bell) wrote

Are default-constructed standard container iterators guaranteed to
compare equal? As in

Is there a requirement that such iterators can be default-constructed at
all?

Yes. A Random Access Itertor is a refinement of Bidirectional
Iterator which is a refinement of Forward Iterator which is a
refinement of Input Iterator which is a refinement of Trivial Iterator
which is a refinement of Default Constructible.

I don't know whether you misread SGI or they misspoke. Neither input
(table 72) nor output (table 73) iterator are required to be default
constructable. One of the refinements made by forward (table 74)
iterator is default constructable.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Thu Oct 16, 2003 2:14 pm    Post subject: Re: Iterator Question Reply with quote

On 15 Oct 2003 18:02:12 -0400, Michael Birck <LASTNAME (AT) mail (DOT) dk> wrote:

Quote:
johnchx2 (AT) yahoo (DOT) com (johnchx) wrote in news:4fb4137d.0310140959.3a926535
@posting.google.com:

[email]belvis (AT) pacbell (DOT) net[/email] (Bob Bell) wrote
Is there a requirement that such iterators can be default-constructed at
all?

Yes - otherwise you couldn't put iterators in a standard container.

No. The standard was purposely written to allow non-default
constructable objects in all containers. The requirements are
copy constructable and copy assignable.

// back_insert_iterator<vector i; // error no ctor
vector<back_insert_iterator > v; // valid
vector<int> vi;
v.push_back(back_inserter(vi)); // amusing
v[0] = 42;
assert(vi.size() == 1);

Here is an iterator that is not default constructable; however,
it is not a standard container :: iterator. A standard container
of them is still valid. Some operations, such as reserve, are
not allowed.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Oct 16, 2003 2:25 pm    Post subject: Re: Iterator Question Reply with quote

"Michael Birck" <LASTNAME (AT) mail (DOT) dk> wrote in message
Quote:
johnchx2 (AT) yahoo (DOT) com (johnchx) wrote in news:4fb4137d.0310140959.3a926535

Is there a requirement that such iterators can be default-constructed at
all?

Yes - otherwise you couldn't put iterators in a standard container.

Standard containers can hold objects that have no default constructor. The
following program should compile.

#include <vector>

class Thing {
public:
Thing(int, int);
};

int main() {
std::vector<Thing> vector;
vector.push_back(Thing(1,2));
}

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Oct 16, 2003 2:49 pm    Post subject: Re: Iterator Question Reply with quote

Michael Birck <LASTNAME (AT) mail (DOT) dk> wrote

Quote:
johnchx2 (AT) yahoo (DOT) com (johnchx) wrote in
news:4fb4137d.0310140959.3a926535 @posting.google.com:

Is there a requirement that such iterators can be
default-constructed at all?

Yes - otherwise you couldn't put iterators in a standard container.

The answer is yet, because the standard says so. None of the standard
containers require the contained object to be default constructable,
however, and you can (and I sometimes do) use containers of objects
which do not have default constructors.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
johnchx
Guest





PostPosted: Thu Oct 16, 2003 2:54 pm    Post subject: Re: Iterator Question Reply with quote

[email]usenet_cpp (AT) lehrerfamily (DOT) com[/email] (Joshua Lehrer) wrote
Quote:
johnchx2 (AT) yahoo (DOT) com (johnchx) wrote
Is there a requirement that such iterators can be
default-constructed at
all?


Yes. A Random Access Itertor is a refinement of Bidirectional
Iterator which is a refinement of Forward Iterator which is a
refinement of Input Iterator which is a refinement of Trivial Iterator
which is a refinement of Default Constructible.

Once perhaps, but no longer -- input iterators need not be default
constructable (table 72). The requirement is introduced for forward
iterators (table 74) and "above". Which, for the purposes of my
question, boils down to the same thing, of course.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
johnchx
Guest





PostPosted: Thu Oct 16, 2003 2:54 pm    Post subject: Re: Iterator Question Reply with quote

Michael Birck <LASTNAME (AT) mail (DOT) dk> wrote
Quote:
johnchx2 (AT) yahoo (DOT) com (johnchx) wrote
Is there a requirement that such iterators can be
default-constructed at
all?

Yes - otherwise you couldn't put iterators in a standard container.


The standard containers require their elements to be copy
constructible and assignable, not necessarily default constructible.

The answer to my question is indeed "yes," but that's not the reason.
:-)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.