digitalmars.D.learn - how to definition a non-const pointer that point a const var.
- lili (4/4) Aug 23 2019 Hi:
- Jonathan M Davis (12/16) Aug 23 2019 D uses parens to restrict how much of the type is const.
- Max Haughton (5/21) Aug 23 2019 As to const pointers to mutable types, it can be done in a
- Jonathan M Davis (10/40) Aug 24 2019 You can create a wrapper struct that acts as a pointer and make it so th...
Hi: In C we can definition const int *ncp_to_cv; or int * const cp_to_ncv; How to do this in D.
Aug 23 2019
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
Aug 23 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: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 structHi: 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
Aug 23 2019
On Saturday, August 24, 2019 12:48:33 AM MDT Max Haughton via Digitalmars-d- learn wrote:On Saturday, 24 August 2019 at 05:03:43 UTC, Jonathan M Davis wrote:You can create a wrapper struct that acts as a pointer and make it so that the struct can't be assigned to, making it emulate const, but you can't actually make it const, since that would make the wrapped pointer const as well. Personally, I think that const pointers to mutable data are useless anyway, but IIRC, there's a library somewhere on code.dlang.org that has a struct that tries to emulate such a pointer, since it gets brought up from time to time. - Jonathan M DavisOn Friday, August 23, 2019 10:14:56 PM MDT lili via Digitalmars-d-learn wrote: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 structHi: 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
Aug 24 2019