[lug] OT: C++ question, const_iterator design
stimits at attbi.com
Sun Jun 1 10:17:36 MDT 2003
> >>>>>"DS" == D Stimits writes:
> DS> When I do this it works:
> DS> iterator it_non_const = foo.begin();
> DS> When I do this, it fails:
> DS> const_iterator it_const = foo.begin();
> How about:
> const_iterator it_const = const_cast(foo).begin();
> This is based on the idea that, unless foo is declared to be const,
> then foo.begin is resolved to a call to the plain begin() (not
> "begin() const").
Nope, this is a failure if I must do it that way. The object foo should
return a plain iterator if lhs is iterator, and should return
const_iterator if lhs is const_iterator. I want it to work the way it
should, exactly as STL does. To do it by casting would make it violate
STL container principles. To do it by inheritance is a work-around. I
want to know the root cause, and not just "make it work".
> If that doesn't work, a copy of the actual diagnostic message would be
I'm not looking for something that just works, I'm looking for the way
it should be done to do it exactly as a perfect STL container does it.
The message is a bit more complicated because foo is a template, but for
example foo<string> would say there is no match for
foo<string>::const_iterator & = foo<string>::iterator
Then it names candidates, and tells me I *can* use
foo<string>::const_iterator operator= (foo<string>::const_iterator)
It sees the operator= overload that accepts const_iterator, but it is
calling "iterator begin()" instead of "const_iterator begin() const".
This is wrong, and on the STL it works right, so I know it must be
something I did wrong instead of a g++ bug.
D. Stimits, stimits AT attbi DOT com
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
More information about the LUG