cannot cast

Namespace rswhite4 at googlemail.com
Wed May 2 15:38:35 PDT 2012


Other, shorter example:

[code]
import std.stdio, std.traits;

class A {
	int val;

	alias val this;
	
	T opCast(T : Object)() {
		writeln("FOO");
		
		return to!(T)(this);
	}
}

class B : A {

}

T to(T : Object, U : Object)(const U obj) {
	return *(cast(T*) &obj);
}

T const_cast(T)(const T obj) {
	return cast(T) obj;
}

void main () {
	A a = new B();
	a.val = 42;
	
	writefln("a.val: %d", a.val);

	B* b = cast(B*) &a;
	writefln("*b.val: %d", b.val);

	B b1 = to!(B)(a);
	writefln("b1.val: %d", b1.val);
	
	B b2 = cast(B) a;
	writefln("b2.val: %d", b2.val);
	
	const B b3 = cast(B) a;
	
	B b4 = const_cast(b3);
}
[/code]

print:

alias_this_impl.d(24): Error: function 
alias_this_impl.A.opCast!(B).opCast () is
  not callable using argument types ()
alias_this_impl.d(44): Error: template instance 
alias_this_impl.const_cast!(B) e
rror instantiating

I'm not very skillful in such "template" stories. Maybe someone 
can help me?



More information about the Digitalmars-d-learn mailing list