Sign in to follow this  
Telastyn

STL Iterator question

Recommended Posts

Quote:
Original post by load_bitmap_file
I think it's a tad more efficient than a normal iterator which is nice if you're not going to change anything as DigitalDelusion said.


The efficeny issue is mostly moot and can be considred somewhat a urban legend of programing but making const correct code makes maintnance easier and works to document the code and put your assumptions into the code instead somewhere burried deep in your skull where co-workers and even you fail to find it 6months later.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Why do const_iterator 's exist?


it's to do with constant corrrect-ness, if your doing an operation that does not modify state you should use constant iterators/references etc. Also if you try to do this:


#include <list>

struct foo {

typedef std::list<int> lints

lints bar;

//....

void do_it() const {

lints::iterator itr = bar.begin(); //whoops, compiler shoot him
//....
}
};


Your compiler will shoot you unless bar is mutable data member (not generally a good idea unless you genuinely need to modify state in a constant member function) thats when you use constant iterators. Constant iterators will generally contain some form of pointer to constant node or what-ever depends on the container.

Also if you have constant data (or constant reference or pointer to constant data) you can only invoke constant member functions on it hence 2 versions of begin and end both constant and non constant member functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Why do const_iterator 's exist?


It's the difference between a const pointer and a pointer to const. A const pointer isn't, itself, allowed to change, but the pointee can be modified, while you can make a pointer to const point to different things at different time, but those things cannot, themselves, be modified.

T* const fooconst container::iterator foo;
const T* foocontainer::const_iterator foo;

Share this post


Link to post
Share on other sites
Heh, perhaps being more up to snuff on non-STL constness would help eh?

So it is, in essence, a tool to enable programmers to shackle themselves into not doing something dumb, akin to private class members?

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
So it is, in essence, a tool to enable programmers to shackle themselves into not doing something dumb, akin to private class members?


Yes. And constness may have performance implications: the compiler knows that the value isn't going to change.

You'll also need a const_iterator if your container is itself const.

Share this post


Link to post
Share on other sites
Constness does have performance implications. The compilers optimization routines can make certain assumptions about the expressions and statements in regards to refactoring, constant propagation, as well as moving variables into and out of scope to enhance performance. At least from what I understand about compilers and their algorithms. As far as why they exist everyone has chimed in the great answers.

DigitalDelusion: What part of the efficiency of const is moot? Where are the trade offs vs not going const? I'm not attacking you but merely curious.

Share this post


Link to post
Share on other sites
Well there's not any drawbacks on using const, being const correct is a virtue in it's own and serves as valuble documentation. But implying that const correctness would give any significant performance gains is for the most part utter crap, yes there are instances where it will help a bit but I wouldn't consider performance an argument for being const correct. Maybe I should have phrased it diffrently but simply put const won't magicly make your code faster in most cases, although it definatly won't hurt. The biggest problem still remains and that's the problem with aliasing issues and for the most part no amount of const correctness will help you with that.

But you really don't have to trust me you can ask Herb Sutter instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this