PrimitiveRef ?

Andre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 23 22:19:11 PDT 2015


Thanks a lot.

I read it in the D Cookbook from Adam D. Ruppe.
In the chapter of memory management there is a topic,
how to build reference counted objects. Here this
construct is explained.

Kind regards
André

On Monday, 23 March 2015 at 20:58:48 UTC, Namespace wrote:
> Something like that?
>
> struct PrimitiveRef(T)
> {
> 	private T* _value;
>
> 	@property
> 	ref inout(T) get() inout pure nothrow {
> 		assert(_value);
> 		
> 		return *_value;
> 	}
> 	
> 	alias get this;
> 	
> 	this(T val) {
> 		_value = new T(val);
> 	}
> }
>
> alias BoolRef = PrimitiveRef!bool;
>
> void test(BoolRef b)
> {
> 	b = true;
> }
>
> void main()
> {
> 	BoolRef b = false;
> 	test(b);
> 	assert(b == true);	
> }



More information about the Digitalmars-d-learn mailing list