Is there a way to make a function parameter accept only values that can be checked at compile time?
max haughton
maxhaton at gmail.com
Tue Dec 28 22:26:33 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?
Why do you need this? What's wrong with a normal branch in this
case.
More information about the Digitalmars-d-learn
mailing list