Compile time int to string conversion in BetterC

Dennis dkorpel at gmail.com
Wed Aug 17 10:38:33 UTC 2022


On Wednesday, 17 August 2022 at 08:44:30 UTC, Ogi wrote:
> Maybe I’m missing something?

I had the same problem, and came up with the following trick:

```D
enum itoa(int i) = i.stringof;

enum major = 3;
enum minor = 2;
enum patch = 1;

enum versionString = itoa!major ~ "." ~ itoa!minor ~ "." ~ 
itoa!patch;

static assert(versionString == "3.2.1");
```

Now I need to warn you that the output of `stringof` is 
technically implementation defined per the specification, so you 
shouldn't rely on it. In practice [this doesn't stop 
people](https://github.com/libmir/mir-algorithm/pull/422), and I 
don't think integers will ever not be printed as a string of base 
10 digits.


More information about the Digitalmars-d-learn mailing list