What guarantees does D 'const' provide, compared to C++?

Peter Alexander peter.alexander.au at gmail.com
Sat Aug 18 04:25:56 PDT 2012


On Saturday, 18 August 2012 at 04:18:33 UTC, Jesse Phillips wrote:
> On Friday, 17 August 2012 at 22:05:56 UTC, Walter Bright wrote:
>> On 8/17/2012 2:34 PM, Mehrdad wrote:
>>> On Friday, 17 August 2012 at 20:46:02 UTC, Walter Bright 
>>> wrote:
>>>> On 8/16/2012 6:43 PM, Mehrdad wrote:
>>>>> On Friday, 17 August 2012 at 01:25:18 UTC, Chris Cain wrote:
>>>>>> Yeah. Again, you can't modify __the const view__.
>>>>>
>>>>> Isn't that kinda useless, if it tells you nothing about the 
>>>>> object itself?
>>>>
>>>> It means you can write code that can process both mutable 
>>>> and immutable objects.
>>>
>>> I meant from a C++/D comparison standpoint...
>>
>> I don't know what you're driving at.
>
> He wants to know what optimizations you get from transitive 
> const over C++ const when you ignore other D features such as 
> immutable and pure.

In D, const without immutable is meaningless.

const on its own provides no guarantees, it just imposes 
restrictions so that immutable can provide guarantees.

Think of it this way: const without immutable is like an 
interface with only one implementation. It's pointless: using the 
interface would just deny you access to other details of the 
(only) implementation. However, the restrictions provided by the 
interface (i.e. that the implementations must have certain 
methods) allows other implementations, and then using the 
interface is a useful way of writing polymorphic code.

const is the interface to mutable and immutable.


More information about the Digitalmars-d mailing list