template type deduction of static 2d arrays

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 24 16:11:34 PDT 2014


On Fri, 24 Oct 2014 22:32:57 +0000
uri via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

D doesn't try to convert types on template argument deduction. what you
really get from '[[1,2],[3,4]]' is (int[][]), not (int[2][2]). note
that static arrays are not the same thing as dynamic arrays. for 'f1'
compiler doing this behind the curtains: 'cast(int[2][2])[[1,2],[3,4]]'.
but for 'f2' compiler can't do this, it must use the type that was
given to it. so it tries template with 'int[][]' and fails.

you can either change your template to `auto f2(T)(T[][] m)` (so it
accepts dynamic arrays', or explicitly cast your array literal:
`res = f2(cast(int[2][2])[[1,2],[3,4]]);`.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141025/e2b9de1c/attachment.sig>


More information about the Digitalmars-d-learn mailing list