Template return values?

Era Scarecrow rtcvb32 at yahoo.com
Mon Dec 3 23:48:19 PST 2012


  A thought's been going through my head for a while now. I wonder 
about template return values. Function signatures are usually the 
inputs and NOT the return type. However for writing a variant 
type could be so very useful. So unless I've misunderstood, this 
doesn't work (but it would be cool if it did).

struct Variant {
   enum ValueType {Long, Float, Double}

   ValueType vt;
   union {
     long lng;
     float fl;
     double dbl;
   }

   T getValue(T)() @property
   if (T == long || T == float || T == double) { //allowable types
     static if (T == long) { //only long case written for brevity
       switch(vt) {
         //entries would likely be mixed in and not written 
manually...
         case Long: return lng;
         case Float:
           assert(lng >= float.min && lng <= float.max);
           return cast(float) lng;
         case Float:
           assert(lng >= double.min && lng <= double.max);
           return cast(double) lng;
       }
     }

     assert(0, "Fell through, should never get here");
   }
}


  So then in theory

  Variant v;
  int i = v.getValue; //calls getValue!int()


More information about the Digitalmars-d-learn mailing list