Cast Object - get null

Jonathan M Davis jmdavisProg at gmx.com
Wed Apr 18 17:37:54 PDT 2012


On Wednesday, April 18, 2012 19:43:07 Jonathan M Davis wrote:
> If you declare another overload of opCast:
> 
> Object opCast(T)() const
> if(is(Unqual!T == Object))
> {
> return this;
> }
> 
> then it should work.

Actually, this would be better:

 T opCast(T)()
 if(isImplicitlyConvertible!(typeof(this), T))
 {
 return this;
 }

 const(T) opCast(T)() const
 if(isImplicitlyConvertible!(typeof(this), const T))
 {
 return this;
 }

The other only works with Object and won't work with any other base classes if 
there are any. It also probably doesn't deal with const very well, and I'm 
surprised that it compiled. It seems like bug, since otherwise, it strips away 
const, which isn't good.

In either case, I believe that this version works and is more flexible. 
Ideally, it wouldn't be necessary though.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list