Declare const a pointer to int?

In C ++, we have the following:

int* p1; // pointer to int const int* p2; // pointer to constant int int* const p3; // constant pointer to int const int* const p4; // constant pointer to constant int 

and in D:

 int* p1; // pointer to int const(int)* p2; // pointer to constant int ?? ?? ?? // constant pointer to int const(int*) p4; // constant pointer to constant int 

what is the syntax for constant pointer to int ?

+6
source share
2 answers

I think you can imitate it:

 struct Ptr(T) { T* _val; this(T* nval) const { _val = nval; } @property T* opCall() const { return cast(T*)_val; } alias opCall this; } void main() { int x = 1; int y = 2; const Ptr!int ptrInt = &x; assert(*ptrInt == 1); *ptrInt = y; // ok assert(*ptrInt == 2); assert(x == 2); ptrInt = &y; // won't compile, good. } 
+5
source

Source: https://habr.com/ru/post/903723/


All Articles