cannot cast
Namespace
rswhite4 at googlemail.com
Wed May 2 13:38:45 PDT 2012
Can anyone tell me, why the this code
[code]
module RefTest.Ref;
import std.stdio : writeln;
import std.conv : to, toImpl;
T const_cast(T : Object)(const T obj) {
return cast(T) obj;
}
struct Ref(T : Object) {
private:
T _obj;
public:
@disable
this();// { }
@disable
this(typeof(null));// { }
this(T obj) {
assert(obj !is null, "Object is null!");
this._obj = obj;
}
@property
inout(T) access() inout {
assert(this._obj !is null, "Access: Object is null!");
return this._obj;
}
//alias access this; // dann kommt "Stackoverflow" oder
"recursive expansion"
}
mixin template TRef(T : Object) {
final Ref!(T) getRef(string file = __FILE__, size_t line =
__LINE__) in {
assert(this !is null, "Object is null! @ " ~ file ~ " in Line "
~ to!(string)(line) ~ ".");
} body {
return Ref!(T)(this);
}
final Ref!(const T) getRef(string file = __FILE__, size_t line =
__LINE__) const in {
assert(this !is null, "Object is null! @ " ~ file ~ " in Line "
~ to!(string)(line) ~ ".");
} body {
return Ref!(const T)(this);
}
U opCast(U : Object)() {
return *(cast(U*) &this);
}
alias getRef this;
}
unittest {
bool instanceof(T : Object, U : Object)(const Ref!U obj) {
//return const_cast(obj.access).toString() ==
typeid(T).toString();
const U o = obj.access;
return const_cast(o).toString() == typeid(T).toString();
}
class A {
mixin TRef!(A);
}
class B : A { }
class C : B { }
A a1 = new B();
A a2 = new C();
assert(instanceof!(A)(a1) == false);
assert(instanceof!(B)(a1));
assert(instanceof!(C)(a1) == false);
writeln(a1);
B b1 = cast(B) a1;
writeln(b1);
writeln();
}
[/code]
fails with:
[quote]
Fehler 3 instantiated from here:
instanceof!(A,A) D:\D\VisualD\Visual Studio
2010\Projects\RefTest\RefTest\Ref.d 76
Fehler 2 Error: template instance RefTest.Ref.const_cast!(A)
error instantiating D:\D\VisualD\Visual Studio
2010\Projects\RefTest\RefTest\Ref.d 62
Fehler 4 Error: template instance
RefTest.Ref.__unittest1.instanceof!(A,A) error
instantiating D:\D\VisualD\Visual Studio
2010\Projects\RefTest\RefTest\Ref.d 76
Fehler 1 Error: function
RefTest.Ref.__unittest1.A.TRef!(A).opCast!(A).opCast () is not
callable using argument types () D:\D\VisualD\Visual Studio
2010\Projects\RefTest\RefTest\Ref.d 7
[/quote]
? Sounds like a bug.
More information about the Digitalmars-d-learn
mailing list