www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CTFE static array error: cannot modify read-only constant

reply Johan <j j.nl> writes:
Hi all,
```
   auto foo(const int[3] x)
   {
       int[3] y = x;
       y[0] = 1; // line 4
       return y;
   }
   immutable int[3] a = [0,1,2];
   immutable int[3] b = foo(a); // line 8
```
compiles with an error:
```
4: Error: cannot modify read-only constant [0, 1, 2]
8:        called from here: foo(a)
```

What am I doing wrong?

Thanks,
   Johan
Sep 22 2017
parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Friday, 22 September 2017 at 14:43:28 UTC, Johan wrote:
 Hi all,
 ```
   auto foo(const int[3] x)
   {
       int[3] y = x;
       y[0] = 1; // line 4
       return y;
   }
   immutable int[3] a = [0,1,2];
   immutable int[3] b = foo(a); // line 8
 ```
 compiles with an error:
 ```
 4: Error: cannot modify read-only constant [0, 1, 2]
 8:        called from here: foo(a)
 ```

 What am I doing wrong?

 Thanks,
   Johan
Looks like a compiler bug to me. Please file a bug report on https://issues.dlang.org/enter_bug.cgi
Sep 23 2017