Template argument deduction

Ali Çehreli acehreli at yahoo.com
Mon Feb 28 21:35:50 PST 2011


On 02/28/2011 07:39 PM, Tom wrote:
> I have...
>
> import std.stdio;
>
> int main(string[] args) {
> foo([[1,2],[3,4],[5,6]]); // ERROR [1]
> bar([[1,2],[3,4],[5,6]]); // OK
> foo!int([[1,2],[3,4],[5,6]]); // OK
>
> return 0;
> }
>
> void foo(T)(T[2][] t) {
> writeln(typeid(t));
> }
>
> void bar(T)(T[][] t) {
> writeln(typeid(t));
> }
>
> [1]
> src\main.d(4): Error: template main.foo(T) does not match any function
> template declaration
> src\main.d(4): Error: template main.foo(T) cannot deduce template
> function from argument types !()(int[][])
>
>
> Why can't compiler deduce template parameters from arguments in the
> first instantiation?
>
> Thanks in advance,
> Tom;

That's because the type of literals like [1, 2] are slices (dynamic 
arrays), not fixed-sized arrays.

import std.stdio;

void main()
{
     writeln(typeof([1,2]).stringof);
}

The output of that program is

int[]

Ali



More information about the Digitalmars-d-learn mailing list