Templates: generic "return null;"

Chris wendlec at tcd.ie
Mon Feb 3 02:25:17 PST 2014


Is there a way I can make the return type in getAttribute 
generic? null does not work with numbers.

MyStruct(T) {
   T[T] attributes;
   // ....
   public auto getAttribute(T attr) {
       if (!(attr in attributes)) {
         return null; // Doesn't work for numbers!
       }
       return attributes[attr];
     }
}

void main() {
   auto myStr = MyStruct!int(0); // Error
}


More information about the Digitalmars-d-learn mailing list