April 19, 2012
On Wednesday, 18 April 2012 at 17:28:16 UTC, Jonathan M Davis wrote:
> Ideally, you'd also have a template constraint restricting the cast to the
> types that you want to be able to cast to, but since you're dealing with a
> templated type, that can be a bit tricky. One (somewhat ugly) option would be
> to do something like
>
> 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 new U(x, y);
> }
>
> Another would be to have an enum on the type indicating that it's an
> instantiation of your Vector2D template (e.g. isVector2D).
>
> U opCast(U) const
>  if(__traits(compiles, U.isVector2D))
> {
>  return new U(x, y);
> }
>
> - Jonathan M Davis

T opCast(T : const(Vector2D!U), U)() const
{
    return new T(x, y);
}

April 19, 2012
> T opCast(T : const(Vector2D!U), U)() const
> {
>     return new T(x, y);
> }

Awesome, now i only need this three and it works perfect.

typeof(this) opCast(T)() if (isImplicitlyConvertible!(typeof(this), T)) {
	writeln("implicit cast");
	return this;
}

const(typeof(this)) opCast(T)() const if (isImplicitlyConvertible!(typeof(this), const T)) {
	writeln("implicit const cast");
	return this;
}

T opCast(T : const(Vector2D!(U)), U)() const {
	writeln("Other cast");
	return T(x, y);
}

Many thanks to you two! :)


April 19, 2012
On Thursday, April 19, 2012 23:06:58 Jakob Ovrum wrote:
> T opCast(T : const(Vector2D!U), U)() const
> {
> return new T(x, y);
> }

Cool. That's definitely better. I definitely need to improve my template-foo with regards to :.

- Jonathan M Davis
1 2 3 4
Next ›   Last »