www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ctfe and static arrays

reply "Jay Norwood" <jayn prismnet.com> writes:
I'm a bit confused by the documentation of the ctfe limitations 
wrt static arrays due to these seemingly conflicting statements, 
and the examples didn't seem to clear anything up.  I was 
wondering if anyone has examples of clever things that might be 
done with static arrays and pointers using ctfe.

2.Executed expressions may not reference any global or local 
static variables.

C-style semantics on pointer arithmetic are strictly enforced. 
Pointer arithmetic is permitted only on pointers which point to 
static or dynamic array elements.
May 24 2015
parent reply "anonymous" <anonymous example.com> writes:
On Sunday, 24 May 2015 at 17:35:39 UTC, Jay Norwood wrote:
 I'm a bit confused by the documentation of the ctfe limitations 
 wrt static arrays due to these seemingly conflicting 
 statements, and the examples didn't seem to clear anything up.  
 I was wondering if anyone has examples of clever things that 
 might be done with static arrays and pointers using ctfe.

 2.Executed expressions may not reference any global or local 
 static variables.

 C-style semantics on pointer arithmetic are strictly enforced. 
 Pointer arithmetic is permitted only on pointers which point to 
 static or dynamic array elements.
"Static array" has a special meaning. It does not mean "static variable with an array type". Static arrays are those of the form Type[size]. That is, the size is known statically. Examples: 1) static int[5] x; -- x is a static variable with a static array type 2) static int[] x; -- static variable, dynamic array 3) int[5] x; -- non-static variable, static array 4) int[] x; -- non-static variable, dynamic array So, CTFE can't handle examples 1 and 2, because they're static variables. 3 and 4 are fine.
May 24 2015
next sibling parent "anonymous" <anonymous example.com> writes:
On Sunday, 24 May 2015 at 18:14:19 UTC, anonymous wrote:
 "Static array" has a special meaning. It does not mean "static 
 variable with an array type". Static arrays are those of the 
 form Type[size]. That is, the size is known statically.
PS: You may also see the term "fixed-size array" which means the same as "static array" but avoids the confusion with static variables.
May 24 2015
prev sibling parent reply "Jay Norwood" <jayn prismnet.com> writes:
On Sunday, 24 May 2015 at 18:14:19 UTC, anonymous wrote:

 "Static array" has a special meaning. It does not mean "static 
 variable with an array type". Static arrays are those of the 
 form Type[size]. That is, the size is known statically.

 Examples:

 1) static int[5] x; -- x is a static variable with a static 
 array type
 2) static int[] x; -- static variable, dynamic array
 3) int[5] x; -- non-static variable, static array
 4) int[] x; -- non-static variable, dynamic array

 So, CTFE can't handle examples 1 and 2, because they're static 
 variables. 3 and 4 are fine.
From your description, I would expect this to fail since I would expect it to be included in 2 above, but it builds and prints ok. import std.stdio; struct A { int me; int next; int prev;} A[] initA(int n) { if (!__ctfe){ assert(false); } A[] v = new A[n]; foreach (i; 0..n){ v[i].me = i; v[i].prev = i-1; v[i].next = i+1; } return v; } int main(string[] argv) { enum int N = 100; static A[] linkedA = initA(N); writefln("%s",linkedA); return 0; }
May 24 2015
parent "anonymous" <anonymous example.com> writes:
On Sunday, 24 May 2015 at 20:53:03 UTC, Jay Norwood wrote:
 On Sunday, 24 May 2015 at 18:14:19 UTC, anonymous wrote:
[...]
 1) static int[5] x; -- x is a static variable with a static 
 array type
 2) static int[] x; -- static variable, dynamic array
 3) int[5] x; -- non-static variable, static array
 4) int[] x; -- non-static variable, dynamic array

 So, CTFE can't handle examples 1 and 2, because they're static 
 variables. 3 and 4 are fine.
From your description, I would expect this to fail since I would expect it to be included in 2 above, but it builds and prints ok. import std.stdio; struct A { int me; int next; int prev;} A[] initA(int n) { if (!__ctfe){ assert(false); } A[] v = new A[n]; foreach (i; 0..n){ v[i].me = i; v[i].prev = i-1; v[i].next = i+1; } return v; } int main(string[] argv) { enum int N = 100; static A[] linkedA = initA(N);
I'm guessing you mean that this line would fall under case 2. Only the right hand side, i.e. initA(N), is done in CTFE here. The result is then used to initialize linkedA -- that step is not CTFE anymore. You're not referencing any static variable in initA, so everything's fine.
May 24 2015