Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread anonymous via Digitalmars-d-learn
On Sunday, 13 March 2016 at 22:34:54 UTC, Chris Wright wrote: In theory, it can't be modified. As a practical matter, unions and casts will allow people to modify it. Saying that it *can't* be modified is slightly besides the point, yeah. It *must* not be modified. Casting away const and then

Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread Chris Wright via Digitalmars-d-learn
On Sun, 13 Mar 2016 21:14:59 +, anonymous wrote: > On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote: >> ref const(Array!Type) view(){} >> >> Unless the result is explicitly cast later it can't me modified. > > No, it can't be modified, period. Casting away const and then mutating >

Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread anonymous via Digitalmars-d-learn
On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote: ref const(Array!Type) view(){} Unless the result is explicitly cast later it can't me modified. No, it can't be modified, period. Casting away const and then mutating is not allowed, it has undefined behavior.

Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread ParticlePeter via Digitalmars-d-learn
On Sunday, 13 March 2016 at 20:28:33 UTC, JR wrote: On Sunday, 13 March 2016 at 20:13:03 UTC, Basile B. wrote: On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote: [...] Basile beat me to it. Yes, ref const(Array!T) accessor. http://dpaste.dzfl.pl/cb2bc5cf9917 Thank you very much,

Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread JR via Digitalmars-d-learn
On Sunday, 13 March 2016 at 20:13:03 UTC, Basile B. wrote: On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote: [...] Basile beat me to it. Yes, ref const(Array!T) accessor. http://dpaste.dzfl.pl/cb2bc5cf9917

Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread Basile B. via Digitalmars-d-learn
On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote: Unless the result is explicitly cast later it can't me modified. import std.stdio, std.container.array; struct Foo { private Array!int arr; ref const(Array!int) view() { return arr; } } void main(string[]

Re: How to return a const handle (view) to a mutable member of an agregate

2016-03-13 Thread Basile B. via Digitalmars-d-learn
On Sunday, 13 March 2016 at 19:37:42 UTC, ParticlePeter wrote: I have a struct that privately warps an std.container.array. I would like to return a read-only reference of this array, it should not be duplicated. How can I do this? Cheers, ParticlePeter ref const(Array!Type) view(){}