Bug 5127 – Template instantiation arguments with CTFE on expressions

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-10-28T14:42:11Z
Last change time
2024-12-13T17:54:01Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#17519 →

Comments

Comment #0 by bearophile_hugs — 2010-10-28T14:42:11Z
This is a correct D2 program. Given an array, doubleit() returns an array with two copies of each item, in this example doubleit() outputs [1, 1, 2, 2, 3, 3, 4, 4], the return type is a twice long array of ints: T[n+n] doubleit(T, int n)(T[n] data) { typeof(return) result; foreach (i, x; data) { result[i * 2] = x; result[i * 2 + 1] = x; } return result; } void main() { int[4] v = [1, 2, 3, 4]; int[8] r = doubleit(v); } This is a similar program, but it currently doesn't compile: void doubleit(T, int n)(T[n] data, out T[n+n] result) { foreach (i, x; data) { result[i * 2] = x; result[i * 2 + 1] = x; } } void main() { int[4] v = [1, 2, 3, 4]; int[8] r; doubleit(v, r); } DMD 2.050beta shows the errors: test.d(1): Error: Integer constant expression expected instead of n + n test.d(10): Error: template test2.doubleit(T,int n) does not match any function template declaration test.d(10): Error: template test2.doubleit(T,int n) cannot deduce template function from argument types !()(int[4u],int[8u]) The type inferencing of the template instantiation is able to perform and test n+n for return value length, but it's not currently able to do it for that out argument. This missing capability is generally useful for data structures that are defined on one or more compile-time integer values (as fixed-sized arrays here) (For more info search for "number parameterized types" with Google).
Comment #1 by bearophile_hugs — 2010-10-28T16:34:23Z
Currently the best workaround for that limitation is to use a template constraint: void doubleit(T, int n, int m)(T[n] data, out T[m] result) if (m == n+n) { foreach (i, x; data) { result[i * 2] = x; result[i * 2 + 1] = x; } } void main() { int[4] v = [1, 2, 3, 4]; int[8] r; doubleit(v, r); }
Comment #2 by robert.schadek — 2024-12-13T17:54:01Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17519 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB