Bug 16542 – makeArray not usable with const initializer
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2016-09-25T21:50:10Z
Last change time
2020-03-21T03:56:32Z
Assigned to
Lucia Cojocaru
Creator
Ryan
Comments
Comment #0 by clumsycodemonkey — 2016-09-25T21:50:10Z
example:
---------------------------------------
void doSomething(T)(in T initialValue)
{
T[] t = theAllocator.makeArray!T(100, initialValue); // won't compile
T[] t2 = theAllocator.makeArray!T(100, cast()initialValue); // this one compiles
// ... use t and t2
}
-----------------------------------------
If all you are doing is copying the value, you shouldn't actually be changing initialValue, so why not make it const?
Comment #1 by b2.temp — 2017-01-10T15:32:48Z
Can you add a test case please ? I suspect this report to be either either bad formulated or invalid because with DMD 2.073-b1 the following test passes:
unittest
{
const int[] v;
void test(T)(in T t)
{
auto a = theAllocator.makeArray(100, t);
}
test(v);
}
Comment #2 by clumsycodemonkey — 2017-01-10T16:04:41Z
The code below compiles and runs unless you uncomment the first case. Then it fails on DMD 2.72.2 on mac. I ran this file with 'rdmd -unittest -main test.d'.
It fails to find a template match for the first case, but when you cast away the const, it works fine.
-------------------------------------------
module test;
void doSomething(T)(in T initialValue)
{
import std.experimental.allocator;
import std.stdio;
pragma(msg, typeof(initialValue).stringof ~ " " ~ typeof(cast()initialValue).stringof);
//T[] t = theAllocator.makeArray!T(5, initialValue); // won't compile
T[] t2 = theAllocator.makeArray!T(5, cast()initialValue); // this one compiles
// ... use t and t2
writeln(t2);
}
unittest
{
int i = 5;
int[3] j = [1,2,3];
doSomething(i);
doSomething(j);
}
------------------------------------------------
Thanks,
Comment #3 by github-bugzilla — 2017-10-30T14:22:49Z