how to definition a non-const pointer that point a const var.

Max Haughton maxhaton at gmail.com
Sat Aug 24 06:48:33 UTC 2019


On Saturday, 24 August 2019 at 05:03:43 UTC, Jonathan M Davis 
wrote:
> On Friday, August 23, 2019 10:14:56 PM MDT lili via 
> Digitalmars-d-learn wrote:
>> Hi:
>>    In C we can definition const int *ncp_to_cv;
>>     or int * const cp_to_ncv;
>>    How to do this in D.
>
> D uses parens to restrict how much of the type is const.
>
> const int* - const pointer to const int
> const(int*) - const pointer to const int
> const(int)* - mutable pointer to const int
>
> Similarly,
>
> const(int*)* - mutable pointer to const pointer to const int
> const(int)** - mutable pointer to mutable pointer to const int
>
> D's const is transitive, so it's not possible to have a const 
> pointer to a mutable type.
>
> - Jonathan M Davis

As to const pointers to mutable types, it can be done in a 
library (Final in std.typecons). I don't know what the overhead 
is but I imagine it wraps it in a struct



More information about the Digitalmars-d-learn mailing list