generic + numeric + literals = abomination

Jesse Phillips jessekphillips+D at gmail.com
Sat Mar 27 08:48:51 PDT 2010


I think you would end up creating a scalar class/struct with operator
overloading to get the behavior you are looking for. I realize that is
more overhead than what you would want but I don't see another way.

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
>
> Next i instantiate vectors, matrices etc... from templates.
> typedef vector_t<scalar, 3> vector;
> typedef matrix_t<scalar, 3, 3> matrix;
>
> 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 b = a * scalar(0.9) + scalar(5); // " "
> const vector v = vector(8.0) * scalar(3.0); // can't drop cast, error
> ...
>
> 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!
>


More information about the Digitalmars-d-learn mailing list