How to return a const handle (view) to a mutable member of an agregate
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 13 13:10:57 PDT 2016
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(){}
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[] args)
{
Foo foo;
auto a = foo.view.dup;
}
More information about the Digitalmars-d-learn
mailing list