Cast Object - get null

Namespace rswhite4 at googlemail.com
Thu Apr 19 00:01:15 PDT 2012


On Thursday, 19 April 2012 at 06:56:21 UTC, Jonathan M Davis 
wrote:
> On Thursday, April 19, 2012 08:25:13 Namespace wrote:
>> Wow, many thanks for your trouble. Now it works as aspected.
>> I only need
>> 
>> T opCast(T)() const if(isImplicitlyConvertible!(typeof(this), 
>> T))
>> {
>> 	return this;
>> }
>> 
>> to work.
>> 
>> But if i try to cast to a const like this
>> const Vector2s vs_ = cast(Vector2s)(vf);
>> 
>> I get a long list of compiler errors.
>> Thereby it's petty if i have only
>> 
>> T opCast(T)() const if(isImplicitlyConvertible!(typeof(this), 
>> T))
>> {
>> 	return this;
>> }
>> 
>> or additional the const variant
>> 
>> const(T) opCast(T)() const
>> if(isImplicitlyConvertible!(typeof(this), const T)) {
>> 	return this;
>> }
>
>
> Well, if the function is const (which makes this const), since 
> you're
> returning this, the return type must be const. It can only 
> return non-const if
> the function isn't const. That's why I suggested having both 
> overloads.
>
> - Jonathan M Davis

Yeah, but
const Vector2s vs_ = cast(Vector2s)(vf);

don't work even with

U opCast(U)() const if (is(Unqual!U == Vector2D!byte) ||
	is(Unqual!U == Vector2D!ubyte) ||
	is(Unqual!U == Vector2D!short) ||
	is(Unqual!U == Vector2D!ushort) ||
	is(Unqual!U == Vector2D!int) ||
	is(Unqual!U == Vector2D!uint) ||
	is(Unqual!U == Vector2D!long) ||
	is(Unqual!U == Vector2D!ulong) ||
	is(Unqual!U == Vector2D!float) ||
	is(Unqual!U == Vector2D!double) ||
	is(Unqual!U == Vector2D!real))
{
	return U(this.x, this.y);
}

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

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



More information about the Digitalmars-d-learn mailing list