How make Optional pre determined parameter type without overload function?

Ali Çehreli acehreli at yahoo.com
Sun Nov 29 02:55:02 UTC 2020


On 11/28/20 6:40 PM, Marcone wrote:
> void a(T1, T2)(T1 b, T2 c){
>      // I need parameter "c" optional, but only (String or int). How can 
> I make it without overload function?
> }

Since it's optional, T2 must have a default type. I made it 'int' below.

void a(T1, T2 = int)(T1 b, T2 c = T2.init)
if (is (T2 == string) ||
     is (T2 == int))
{
   // ...
}

void main() {
   a(1.5);
   a(2.5, "hello");
   a(3.5, 42);
   static assert(!__traits(compiles, a(4.5, 5.5)));
}



More information about the Digitalmars-d-learn mailing list