Converting a number to complex

Joshua Niehus jm.niehus at gmail.com
Fri Nov 23 08:11:24 PST 2012


On Friday, 23 November 2012 at 12:39:59 UTC, Frederik Vagner 
wrote:
> Now do it for complex number please ^^

touche!

import std.stdio, std.conv, std.traits, std.complex;

template isComplexNumeric(T)
{
     static if(is(NumericTypeOf!T)) {
         enum bool isComplexNumeric = is(NumericTypeOf!T);
     }
     else static if (is(T == Complex!double))
     {
         enum bool isComplexNumeric = is(Complex!double);
     }
     // fillin real and float here... (e.g. is(Complex!real); 
etc...
}

class Example(T) if (isComplexNumeric!T)
{
     T k = to!T(1);
}

void main()
{
     auto x = new Example!(Complex!double)();
     writeln(x.k);
     auto y = new Example!double();
     writeln(y.k);
     auto z = new Example!string(); // fail
     writeln(z.k);
}

A bit messy, but im sure there is some room for cleanup 
somewhere...




More information about the Digitalmars-d-learn mailing list