Is there a way to make a function parameter accept only values that can be checked at compile time?

Steven Schveighoffer schveiguy at gmail.com
Wed Dec 29 14:49:40 UTC 2021


On 12/29/21 3:55 AM, rempas wrote:

> Thanks! That's cool but I don't want this to be this way. Or at least I 
> want it to be able to take a default value so we don't have to get 
> passed all the time.
OK:

> 
> ```
> extern (C) void main() {
>    void print_num(int mul = 100)(int num) {
>      static if (is(mul == ten)) {
>        printf("%d\n", num * 10);
>      } else static if (is(mul == three)) {
>        printf("%d\n", num * 3);
>      } else {
>        printf("%d\n", num);
>      }
>    }
> 
>    int multi = 211;
>    print_num!3(10);     // Set the value
>    print_num(30);        // Get the default value, have the "else" branch executed
> }
> ```

Template parameters are just like regular parameters, but compile time.

-Steve


More information about the Digitalmars-d-learn mailing list