www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template and constraints

reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
I got a little problem with these code:

____________________
double div(T a, U b)() if( a > 0 && b > 0 ){ // not works T and U unknwn
identifier
    return a / b;
}
double div(T,U)(T a, U b) if( a > 0 && b > 0 ){ // not works, a  and b
not known at compile time
    return a / b;
}

double div(double a, double b)() if( a > 0 && b > 0 ){ // works but not
generic
    return a / b;
}
_____________________


The only one who works is a not generic! they are a way? or i need to
write for each parameter type?
Aug 20 2012
next sibling parent David <d dav1d.de> writes:
Am 20.08.2012 23:52, schrieb bioinfornatics:
 I got a little problem with these code:

 ____________________
 double div(T a, U b)() if( a > 0 && b > 0 ){ // not works T and U unknwn
 identifier
      return a / b;
 }
 double div(T,U)(T a, U b) if( a > 0 && b > 0 ){ // not works, a  and b
 not known at compile time
      return a / b;
 }

 double div(double a, double b)() if( a > 0 && b > 0 ){ // works but not
 generic
      return a / b;
 }
 _____________________


 The only one who works is a not generic! they are a way? or i need to
 write for each parameter type?
Should work with alias, double div(alias a, alias b)() if( a > 0 && b > 0 ){ return a / b; }
Aug 20 2012
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/20/2012 11:52 PM, bioinfornatics wrote:
 I got a little problem with these code:

 ____________________
 double div(T a, U b)() if( a > 0 && b > 0 ){ // not works T and U unknwn
 identifier
      return a / b;
 }
 double div(T,U)(T a, U b) if( a > 0 && b > 0 ){ // not works, a  and b
 not known at compile time
      return a / b;
 }

 double div(double a, double b)() if( a > 0 && b > 0 ){ // works but not
 generic
      return a / b;
 }
 _____________________


 The only one who works is a not generic! they are a way? or i need to
 write for each parameter type?
Alias parameters. auto div(alias a, alias b)().
Aug 20 2012
prev sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Thanks to Timon Gehr and David=20
____
double div(alias a, alias b)() if( a > 0 && b > 0 ){
     return a / b;
}
____

works as expected :-)
Aug 20 2012