View mode: basic / threaded / horizontal-split · Log in · Help
January 21, 2005
Template argument type auto detect
Hello all,

Who can say why impossible to do following

class Class
{
template foo(T)
{
bit foo(T a, T b)
{
return a == b;
}
}
}

int main ( char [] [] args )
{
int x, y;
Class c;
c.foo(x, y); // call

return 0;
}

C:/dmd/bin/dmd -I. -IC:\dmd\src\phobos -Isrc -c -ofexamples\src\test\test.obj
examples\src\test\test.d
examples\src\test\test.d(124): c.foo is not a declaration
examples\src\test\test.d(124): function expected before (), not 'int'

Why compiler can't replase call line with c.foo!(int)(x, y) automaticly like in
C++?
January 21, 2005
Re: Template argument type auto detect
I think you need to 'instanciate' the template with a "!", so foo!() or so. 
Dunno, never done it before.

L.
January 21, 2005
Re: Template argument type auto detect
nail wrote:

>Hello all,
>
>Who can say why impossible to do following
>
>class Class
>{
>  
>
//Templates are like a class/namespace in D

>template foo(T)
>{
>bit foo(T a, T b)
>{
>return a == b;
>}
>}
>}
>
>int main ( char [] [] args )
>{
>int x, y;
>  
>

>Class c;
>  
>
//Should be (something like)
Class.foo!(int) c = new Class.foo!(int)();

>c.foo(x, y); // call
>  
>
There is no automatic type induction in D.

>return 0;
>}
>
>C:/dmd/bin/dmd -I. -IC:\dmd\src\phobos -Isrc -c -ofexamples\src\test\test.obj
>examples\src\test\test.d
>examples\src\test\test.d(124): c.foo is not a declaration
>examples\src\test\test.d(124): function expected before (), not 'int'
>
>Why compiler can't replase call line with c.foo!(int)(x, y) automaticly like in
>C++?
>
>
>  
>
January 21, 2005
Re: Template argument type auto detect
>
>There is no automatic type induction in D.
>

Why? What is the hindrance?
January 22, 2005
Re: Template argument type auto detect
nail wrote:

>>There is no automatic type induction in D.
>>
>>    
>>
>
>Why? What is the hindrance?
>
>
>  
>
1) Walter's against it.
2) It makes the compiler harder to write.
3) There are apparently some un-expected side-effects.

As for me, I would love to have some form of automatic induction for 
templates.

-- 
-Anderson: http://badmama.com.au/~anderson/



-- 
-Anderson: http://badmama.com.au/~anderson/
Top | Discussion index | About this forum | D home