Getting RefCounted to work with classes

uri via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 25 23:01:24 PDT 2014


On Monday, 25 August 2014 at 21:14:03 UTC, Bienlein wrote:
> 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;
> }

RefCounted does not work with classes. Classes are reference 
types already.


The key is in the error message ...if(!is(T == class))... 
restricts the template to non-class types only.

cheers,
uri


More information about the Digitalmars-d-learn mailing list