digitalmars.D.learn - Pass enum variable as const ref arg
- Andrey (3/16) Dec 04 2020 WTF?
- rikki cattermole (4/22) Dec 04 2020 The ref. The problem is the ref. You are passing it a constant, not a
- Andrey (4/4) Dec 04 2020 Hm, you mean that enum variable is not a real variable?
- rikki cattermole (5/9) Dec 04 2020 It is not a variable. It is a constant that cannot be changed and does
- Paul Backus (15/19) Dec 04 2020 The official name for what you're calling an "enum variable" is
- Andrey (1/1) Dec 04 2020 Thank you!
- Bastiaan Veelo (4/13) Dec 04 2020 It works if you pass `-preview=rvaluerefparam` to the compiler.
- Q. Schroll (10/12) Dec 08 2020 If you come from a C or C++ background, it's quite reasonable to
Hello,void test(const ref string[3] qazzz) { qazzz.writeln; } void main() { enum string[3] value = ["qwer", "ggg", "v"]; test(value); }Gives errors:onlineapp.d(26): Error: function onlineapp.test(ref const(string[3]) qazzz) is not callable using argument types (string[3]) onlineapp.d(26): cannot pass rvalue argument ["qwer", "ggg", "v"] of type string[3] to parameter ref const(string[3]) qazzzWTF?
Dec 04 2020
On 05/12/2020 1:54 AM, Andrey wrote:Hello,That is a compile time constant (remove the enum).void test(const ref string[3] qazzz) { qazzz.writeln; } void main() { enum string[3] value = ["qwer", "ggg", "v"];The ref. The problem is the ref. You are passing it a constant, not a variable.test(value); }Gives errors:onlineapp.d(26): Error: function onlineapp.test(ref const(string[3]) qazzz) is not callable using argument types (string[3]) onlineapp.d(26): cannot pass rvalue argument ["qwer", "ggg", "v"] of type string[3] to parameter ref const(string[3]) qazzzWTF?
Dec 04 2020
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?
Dec 04 2020
On 05/12/2020 2:42 AM, Andrey wrote:Hm, you mean that enum variable is not a real variable?It is not a variable. It is a constant that cannot be changed and does not exist in the executable.I thought that to make CT variable you should mark it as enum (in c++ as constexpr). How to do it here?You are already doing it. This is not what you want. You want a variable that will pass by ref. Remove enum.
Dec 04 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
Dec 04 2020
On Friday, 4 December 2020 at 12:54:25 UTC, Andrey wrote:Hello,It works if you pass `-preview=rvaluerefparam` to the compiler. But the other suggestions are better IMO. —Bastiaan.void test(const ref string[3] qazzz) { qazzz.writeln; } void main() { enum string[3] value = ["qwer", "ggg", "v"]; test(value); }Gives errors:
Dec 04 2020
On Friday, 4 December 2020 at 12:54:25 UTC, Andrey wrote:[...] WTF?If you come from a C or C++ background, it's quite reasonable to think of enum stuff like a #define macro. You're using static arrays, but note that array literals allocate in many use cases. It really is like a C/C++ macro. Use n times = allocate n times. You avoid that with a static immutable completely. That said, if you use the value of that enum only at compile-time, there's no need for a static immutable. Hope that this rule of thumb sheds some more light on how to achieve certain stuff.
Dec 08 2020