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

Tobias Pankrath tobias at pankrath.net
Tue Dec 28 21:20:56 UTC 2021


On Tuesday, 28 December 2021 at 21:19:29 UTC, rempas wrote:
> I would like to know if that's possible. Actually I would like 
> to do something like the following:
>
> ```
> extern (C) void main() {
>   void print_num(int num, comp_time_type int mul) {
>     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(10, 3);     // Ok, accept this
>   print_num(10, multi); // Error, do not accept this
> }
> ```
>
> So I want to have "mul" only accept values that can be 
> calculate at compile time so I can use it with things like 
> "static if". Is this possible?

I think you have to make 'mul' a template parameter.


More information about the Digitalmars-d-learn mailing list