www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Counting at compile-time

reply DLearner <bmqazwsx123 gmail.com> writes:
Suppose we have

```
enum Count = 0;

static if (<Compile-time evaluable cond-1>)
    ...

   enum Count = Count + 1; // is disallowed
else

static if (<Compile-time evaluable cond-2>)
    ...

   enum Count = Count + 1; // is disallowed
else

```

and the objective is to determine (at compile time) the number of 
times cond-1 or cond-2 is triggered (perhaps for setting size of 
a fixed array).

So answer is zero, 1 or 2.

But how to get that value into Count?
Jan 26
next sibling parent reply Bradley Chatha <sealabjaster gmail.com> writes:
On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:
 But how to get that value into Count?
It's hard to say what would exactly fit your use case since this is just a minimal example, so it's hard to gauge what other restrictions and inputs exists, but if your logic is a bit more complex then CTFE (Compile Time Function Execution) might be a viable option: ```d int myCount(bool Cond1, bool Cond2)() { // Assumes that the code is more complex, so you can't easily use something similar to: // count = cast(int)Cond1 + cast(int)Cond2; int count = 0; static if(Cond1) count++; static if(Cond2) count++; return count; } pragma(msg, myCount!(false, false)); // 0 pragma(msg, myCount!(true, false)); // 1 pragma(msg, myCount!(false, true)); // 1 pragma(msg, myCount!(true, true)); // 2 // e.g. store it in an enum enum Count = myCount!(true, false); // Or more simply... if possible for your use case enum Cond1 = true; enum Cond2 = false; enum Count = cast(int)Cond1 + cast(int)Cond2; // cast(int)true == 1; cast(int)false == 0 ``` Without additional context it's hard to give a concrete suggestion though, but I hope this helps.
Jan 26
next sibling parent DLearner <bmqazwsx123 gmail.com> writes:
On Sunday, 26 January 2025 at 16:43:23 UTC, Bradley Chatha wrote:
 On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:
 But how to get that value into Count?
It's hard to say what would exactly fit your use case since this is just a minimal example, so it's hard to gauge what other restrictions and inputs exists, but if your logic is a bit more complex then CTFE (Compile Time Function Execution) might be a viable option:
[...]
 Without additional context it's hard to give a concrete 
 suggestion though, but I hope this helps.
Thank you.
Jan 26
prev sibling parent Christian =?UTF-8?B?S8O2c3RsaW4=?= <christian.koestlin gmail.com> writes:
On Sunday, 26 January 2025 at 16:43:23 UTC, Bradley Chatha wrote:
 On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:
 But how to get that value into Count?
It's hard to say what would exactly fit your use case since this is just a minimal example, so it's hard to gauge what other restrictions and inputs exists, but if your logic is a bit more complex then CTFE (Compile Time Function Execution) might be a viable option: ```d int myCount(bool Cond1, bool Cond2)() { // Assumes that the code is more complex, so you can't easily use something similar to: // count = cast(int)Cond1 + cast(int)Cond2; int count = 0; static if(Cond1) count++; static if(Cond2) count++; return count; } pragma(msg, myCount!(false, false)); // 0 pragma(msg, myCount!(true, false)); // 1 pragma(msg, myCount!(false, true)); // 1 pragma(msg, myCount!(true, true)); // 2 // e.g. store it in an enum enum Count = myCount!(true, false); // Or more simply... if possible for your use case enum Cond1 = true; enum Cond2 = false; enum Count = cast(int)Cond1 + cast(int)Cond2; // cast(int)true == 1; cast(int)false == 0 ``` Without additional context it's hard to give a concrete suggestion though, but I hope this helps.
for this usecase (at least the first pragmas) you could also just do a normal function (many "pure" functions qualify as ctfe candidates). So ```d int myCount(bool cond1, bool cond2) { int count = 0; if(cond1) { count++; } if(cond2) { count++; } return count; } ``` would also work.
Jan 28
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Sunday, 26 January 2025 at 15:42:44 UTC, DLearner wrote:
 Suppose we have

 ```
 enum Count = 0;

 static if (<Compile-time evaluable cond-1>)
    ...

   enum Count = Count + 1; // is disallowed
 else

 static if (<Compile-time evaluable cond-2>)
    ...

   enum Count = Count + 1; // is disallowed
 else

 ```

 and the objective is to determine (at compile time) the number 
 of times cond-1 or cond-2 is triggered (perhaps for setting 
 size of a fixed array).

 So answer is zero, 1 or 2.

 But how to get that value into Count?
I have a compile time counter gist, it isnt no where near that clean and it testing the line between a compiler bug
Jan 26