generic + numeric + literals = abomination

Robert Clipsham robert at octarineparrot.com
Sat Mar 27 04:32:24 PDT 2010


On 27/03/10 10:20, so wrote:
> In C++!
>
> I have a type defined in the core library like..
> typedef float scalar;
> //typedef double scalar; // <-- whole framework is now double precision

alias float scalar;
//alias double scalar;

>
> Next i instantiate vectors, matrices etc... from templates.
> typedef vector_t<scalar, 3> vector;
> typedef matrix_t<scalar, 3, 3> matrix;

alias Vector!(scalar, 3) vector; // Presuming you have defined
                                  // the Vector!() template somewhere
alias Matrix!(scalar, 3, 3) matrix; // Presuming Matrix!() is defined

> Until now everything cool, here pain comes...
>
> const scalar a = scalar(0.2) * math::consts<scalar>::pi; // can't drop
> cast, since 0.2 is double

const scalar a = 0.2 * PI; // PI is defined in std.math

> const scalar b = a * scalar(0.9) + scalar(5); // " "

const scalar b = a * 0.9 * 5.0;

> const vector v = vector(8.0) * scalar(3.0); // can't drop cast, error

const vector v = vector(8.0) * 3.0;

> ...
>
> Since D is superb, i like to know how you do it in D.
> If you got a better idea in C++, i would like to hear that too!
>
> Thanks!
>

There are a few vector implementations for D out there, or you can roll 
your own. I'm pretty sure there's matrices out there too, I haven't 
checked though (I'm pretty sure D's built in arrays will do the trick, 
I'm not an expert though).


More information about the Digitalmars-d-learn mailing list