How to check if an array is a manifest constant?
Jonathan M Davis
jmdavisProg at gmx.com
Mon Apr 4 02:00:20 PDT 2011
On 2011-04-04 01:16, simendsjo wrote:
> But there is a difference in how they behave, and I have no way of checking
> this behavior. Consider the following little snippet:
>
> void f(int[] a) {
> a[0] = -1;
> }
>
> void main() {
> int[] a = [1,2,3];
> static assert(is(typeof(a) == int[]));
> f(a);
> assert(a[0] == -1); // a passed by reference, a[0] changed
>
> enum b = [1,2,3];
> static assert(is(typeof(b) == int[])); // a regular int[] you say?
> f(b);
> assert(b[0] == -1); // b passed by value, b[0] unchanged
> }
Okay. I just re-read the appropriate section in TDPL, and it's clear from the
book that it's definitely intended that a new copy of the enum value be
created every time that it's used (I was under the impression that that was
buggy behavior, but apparently that's not true - though the fact that
accessing any array during CTFE results in a copy of that array _is_ buggy
behavior and should be fixed by Don before the next release).
Regardless of that, however, enum values shouldn't be used in any place where
they're going to be altered. So, passing such a value to a function which is
intended to alter the value that it's passed to is just plain a bug on the
part of the caller. It shouldn't matter to the function being called.
That being the case, I don't see why you would need to determine whether a
value that you're dealing with is an enum or not using template constraints or
static if or whatnot. You should know whether the variable that you're using
is an enum or not. If it is, don't alter it until you've set it to a variable
- then you alter the variable.
What use case do you have for wanting to know whether a variable is an enum or
not?
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list