Sugestion for a const implementation

Miles _______ at _______.____
Wed Jan 24 11:01:29 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> That's pretty limiting; you can't define something a la STL begin() that
> returns different objects depending on the object's mutability.

This is almost prejudice against const. You broke my message in this
point just to argue against it, without considering the explanation that
follows.

If you think just a little about the solution, you will see that the
solution cames immediately. iterator<> and const_iterator<> will became
the exact same class, with no distinction between const and non-const.

> Littering declarations is objective. In C++ const is never deduced, only
> checked. The underlying vision is that there should be cases in which
> constness is deduced such that programmers can benefit of it without
> having to babysit the compiler.

I don't share your vision. Doesn't make any sense the compiler deduce const.

So I write a get() method that should only return a value from the
object, but I inadvertently replace a == with a =, or something alike,
and the compiler deduces this is a non-const method, instead of failing
to compile it. So wrong...

A const member function is not "babysitting" the compiler, it is the way
the programmer have to tell the compiler that that function is not
intended to modify the object, so the compiler can aid disallowing any
operation that would affect the object state. Also, it is a
documentation tool.

> Sorry, this is simplistic and incomplete.
> A member function might want to return whatever,

If the same named member function should return different objects, then
you should seriously think renaming them to different names, since they
don't do the same thing.

> including two unrelated types depending on constness.

The only reason I see for returning two "unrelated" (?) types depending
on constness is in situations like STL begin(), and in this specific
situation, there is simply no reason for having two different types like
iterator<> and const_iterator<>. If you have read my message, this would
have came immediately. The conditional const I proposed aids just this.



More information about the Digitalmars-d mailing list