Pass enum variable as const ref arg

Paul Backus snarwin at gmail.com
Fri Dec 4 15:09:57 UTC 2020


On Friday, 4 December 2020 at 13:42:45 UTC, Andrey wrote:
> Hm, you mean that enum variable is not a real variable?
> I thought that to make CT variable you should mark it as enum 
> (in c++ as constexpr).
> How to do it here?

The official name for what you're calling an "enum variable" is 
"manifest constant" [1]. Manifest constants are like named 
literals: when you use one, it is treated by the compiler as 
though you had copy-and-pasted its value at that point in the 
code. So, for example,

     enum string[3] value = ["qwer", "ggg", "v"];
     test(value);

...is equivalent to

     test(cast(string[3]) ["qwer", "ggg", "v"]);

If you want to declare a compile-time constant that's also an 
lvalue, you can use `static immutable` instead of `enum`:

     static immutable string[3] value = ["qwer", "ggg", "v"];
     test(value);

[1] https://dlang.org/spec/enum.html#manifest_constants


More information about the Digitalmars-d-learn mailing list