[cfe-dev] const correctness of iterator methods

Shriramana Sharma samjnaa at gmail.com
Wed Jun 26 15:38:24 PDT 2013


Hi -- again a few questions about the libc++ container templates.

I note that __list_iterator::operator*() returns a non-const reference
but is marked as const. The operator-> next to it returns a non-const
pointer but is also marked const. OTOH in the list class, the begin()
and end() methods returning non-const iterators are NOT marked const
and only those returning const iterators are marked const.

I thought a method should be const if it does not modify the contents
of the object by itself, and not based on whether the value it returns
could be used to modify the object. In this case, it is correct to
mark the iterator's operators * and -> as const because they
themselves do not modify the contents of the list, although their
return value could definitely be used for that. But why is then
begin() returning a non-const iterator NOT marked as const, given that
it itself doesn't modify the contents of the list in any way?

Thanks!

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा




More information about the cfe-dev mailing list