example for compile time function execution

David B. Held dheld at codelogicconsulting.com
Sun Apr 29 21:51:12 PDT 2007


Walter Bright wrote:
> I keep racking my brain trying to come up with a short, pithy example of 
> CTFE that uses AAs or struct literals. It should be just a few lines, 
> and make it clear why CTFE is a great thing. I want to use it in 
> presentations.
> 
> Any ideas?

struct CartesianPoint
{
     double x;
     double y;
};

struct PolarPoint
{
     double r;
     double theta;
};

CartesianPoint convert(PolarPoint pp)
{
     return CartesianPoint(pp.r * cos(pp.theta), pp.r * sin(pp.theta));
}

const CartesianPoint p = convert({ 2, 0.75 * pi });

I don't know how struct literals work, or if this is even a good 
example, but maybe something along these lines would be useful?

Dave



More information about the Digitalmars-d mailing list