Getting RefCounted to work with classes

Bienlein via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 25 14:14:02 PDT 2014


Hello,

the code below compiles and runs fine. However, when I change 
Payload from struct to class I get compiler errors:

Error	1	Error: template instance 
std.typecons.RefCounted!(Payload, 
cast(RefCountedAutoInitialize)1) does not match template 
declaration RefCounted(T, RefCountedAutoInitialize autoInit = 
RefCountedAutoInitialize.yes) if (!is(T == 
class))	C:\Users\Nutzer\Windows Ordner\Documents\Visual Studio 
2013\Projects\RefCountedScratch\RefCountedScratch\main.d	26	

I tried many things, but nothing did it. Any help appreciated :-).
Thanks, Bienlein


import std.stdio;
import std.typecons;

struct Payload
{
	private int num = 0;

	this(int i)
	{
		num = i;
		writefln("Payload's constructor called");
	}

	~this()
	{
		 writefln("Payload's destructor called");
	}
}



int main(string[] argv)
{
     alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data;

     int bar = 12;
     Data data = Data(bar);

     return 0;
}


More information about the Digitalmars-d-learn mailing list