struct on heap with std.experimental.allocator

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 7 08:39:59 PDT 2016


I modified one of the first examples from 
std.experimental.allocator to put a struct on the heap (below).

My sense is that putting it on the GC heap gives the struct 
reference semantics. I was struck by two things. First, I didn't 
have to use (*a).x, I could just use a.x. Also, when I assign a 
struct object to another instance, changes in the new one also 
effect the original.


import std.experimental.allocator;

struct A
{
	int x;
}

void main()
{
	A* a = theAllocator.make!A(42);

	assert((*a).x == 42);
	assert(a.x == 42);
	
	auto b = a;
	assert(b.x == 42);
	++b.x;
	assert(b.x == 43);
	assert(a.x == 43);
}


More information about the Digitalmars-d-learn mailing list