How make Optional pre determined parameter type without overload function?

Marcone marcone at email.com
Sun Nov 29 03:15:04 UTC 2020


On Sunday, 29 November 2020 at 02:55:02 UTC, Ali Çehreli wrote:
> 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)));
> }

Thank you! work fine!


More information about the Digitalmars-d-learn mailing list