const objects

Dom DiSc dominikus at scherkl.de
Sun Aug 17 13:21:34 UTC 2025


If I do:
```d
struct S1
{
    const int* my_x;
    this(const int* x) { my_x = x; }
}

struct S2
{
    int* my_x;
    this(int* x) { my_x = x; }
}

main()
{
    immutable int i = 5;
    S1 s = S1(&i); // works
    immutable S2 t = S2(&i); // fails
}
```

Shoudn't S2 also work? It's immutable, so even if it takes only a 
mutable parameter, it is ensured it will not change it, because 
it is itself immutable, so none of its attributes can be modified 
nor can non-const member functions be called.

Is there a deeper reason why this is not allowed? Or is it simply 
not implemented?



More information about the Digitalmars-d-learn mailing list