any news on const/invariant?
Steven Schveighoffer
schveiguy at yahoo.com
Wed Nov 28 20:02:06 PST 2007
"Walter Bright" wrote
> Janice Caron wrote:
>>> Also, C++ has const as both a storage class and a type constructor, and
>>> yes, they have subtly different meanings. This doesn't seem to cause any
>>> major problems.
>>
>> That's because C++ behaves as expected. If I declare
>>
>> const int * f();
>>
>> then I get a function which returns a pointer to const int, /not/ a
>> const member function which returns a pointer to mutable int.
>
> What is expected, or what you are *used* to? Let me put it another way,
> can you explain the difference between C++ const storage class and C++
> const type constructors? There's a big semantic difference.
Let me reword Walter's point, as I think he didn't really get it across
completely :)
Let's say someone had never used C++ and it's const system. Let's say that
person is a virgin coder starting to dabble in D. Let's explain how const
works to him/her:
to declare that a type is constant, you do:
const(Type) x;
to declare that a method returns a const value, you do:
const(Type) f();
to declare that a method is constant, that is, it will not modify any
members of a class the method resides in, you do:
const Type f();
If that method returns a const type, then the method is declared as:
const const(Type) f();
for compatibility with C++ and C, you can also do:
const Type x;
for a const type declaration, and
Type f() const
for a const method declaration.
To someone who never used const before in another language, this isn't any
less or more intuitive than the way C++ does it. It's like saying using
semicolons at the end of statements is intuitive. It's just a different way
of expressing what you want to the compiler.
-Steve
More information about the Digitalmars-d
mailing list