www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cas function issue

reply Adrian Mercieca <amercieca-remove-this gmail.com> writes:
Hi folks,

I've got this very simple program, for which I keep getting compiler errors at
the 'cas' function call.

import
	std.stdio,
	core.atomic;

class Int
{
public:
	this(int v) {
		this._v = v;
	}

private:
	int _v;
}


void main()
{
	shared(Int) a = new shared(Int)(10);
	shared(Int) b = new shared(Int)(20);
	shared(Int*) p = &a;

	if ( cas(p, a, b) ) {
		writeln("Swap OK");
	}
}

The errors are:

Error: template core.atomic.cas(T,V1,V2) if (__traits(compiles,mixin("*here =
writeThis"))) does not match any
function template declaration

Error: template core.atomic.cas(T,V1,V2) if (__traits(compiles,mixin("*here =
writeThis"))) cannot deduce template
function from argument types !()(shared(Int*),shared(Int),shared(Int))

Is it not possible to compare and swap object references in the cas function
call?
Would someone kindly tell me as to what I am doing wrong?

Thanks.
- Adrian.
Dec 29 2011
parent Adrian Mercieca <amercieca gmail.com> writes:
On Thu, 29 Dec 2011 08:01:51 +0000, Adrian Mercieca wrote:

 Hi folks,
 
 I've got this very simple program, for which I keep getting compiler
 errors at the 'cas' function call.
 
 import
 	std.stdio,
 	core.atomic;
 
 class Int
 {
 public:
 	this(int v) {
 		this._v = v;
 	}
 
 private:
 	int _v;
 }
 
 
 void main()
 {
 	shared(Int) a = new shared(Int)(10);
 	shared(Int) b = new shared(Int)(20);
 	shared(Int*) p = &a;
 
 	if ( cas(p, a, b) ) {
 		writeln("Swap OK");
 	}
 }
 
 The errors are:
 
 Error: template core.atomic.cas(T,V1,V2) if
 (__traits(compiles,mixin("*here = writeThis"))) does not match any
 function template declaration
 
 Error: template core.atomic.cas(T,V1,V2) if
 (__traits(compiles,mixin("*here = writeThis"))) cannot deduce template
 function from argument types !()(shared(Int*),shared(Int),shared(Int))
 
 Is it not possible to compare and swap object references in the cas
 function call? Would someone kindly tell me as to what I am doing wrong?
 
 Thanks.
 - Adrian.
Hi, I think I got it; the class (Int) needs to be declared as shared. Thanks.
Dec 29 2011