Using mixin in array declarations

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 19 01:49:48 PST 2016


On 19/11/2016 10:46 PM, Marduk wrote:
> In C one can do the following:
>
> # define N 10
>
> double M[N][N];
>
>
> In D I would like to achieve the same result. I tried with:
>
> mixin("int N = 10;");
>
> double[N][N] M;
>
>
> but the compiler (DMD) complained with Error: variable N cannot be read
> at compile time.
>
> What am I doing wrong?

enum N = 10;

double[N][N] M;

enum is a constant available for use at compile time, your int there is 
a runtime variable not accessible at runtime.


More information about the Digitalmars-d-learn mailing list