www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - template arguments deduction

reply Maagar <Maagar_member pathlink.com> writes:
Right now D does not support template argument deduction, so its interesting if
this feature is planned at all?

In C++ compiler can guess types and automatically instantiate template, it would
be very nice to have this feature in D.

example (c++):

template <typename T> void foo(T arg)
{
}
..
f(5);
f(5.0);
f('c');

while in D it would be something like:

TFoo!(int).f(5);
TFoo!(float).f(5.0);
TFoo!(char).f('c');
May 19 2004
parent reply Daniel Horn <hellcatv hotmail.com> writes:
you could alias it for all your types

alias TFoo(int) foo;
alias TFoo(float) foo;
alias TFoo(char) foo;

but then you're back to C++ template instantiation madness
I agree it makes userland code a bit messy to have the !() everywhere 
when you wish the compiler would instantiate it for you--
but it is also a big can of worms with respect to overload resolution 
and so forth I suspect

Maagar wrote:

 Right now D does not support template argument deduction, so its interesting if
 this feature is planned at all?
 
 In C++ compiler can guess types and automatically instantiate template, it
would
 be very nice to have this feature in D.
 
 example (c++):
 
 template <typename T> void foo(T arg)
 {
 }
 ..
 f(5);
 f(5.0);
 f('c');
 
 while in D it would be something like:
 
 TFoo!(int).f(5);
 TFoo!(float).f(5.0);
 TFoo!(char).f('c');
 
 
 

May 19 2004
next sibling parent Genki Takiuchi <takiuchi ntechnology.co.jp> writes:
Hi.

If compiler found the code like this:

Foo!(arg1);
Foo!(arg1, arg2);

it is easy to interpret automatically like this:

Foo!(typeof(arg1)).Foo(arg1);
Foo!(typeof(arg1), typeof(arg2)).Foo(arg1, arg2);

So, how about introducing new statement for template
functions like this?

// template function definition
template(T1, T2) void func(T1 arg1, T2 arg2){ ... }
// this is automatically interpreted as:
// template func(T1, T2) {
//     void func(T1 arg1, T2 arg2) { ... }
// }

// call template function
func!(arg1, arg2);
// this is automatically interpreted as:
// func!(typeof(arg1), typeof(arg2)).func(arg1, arg2);

thank you.


Daniel Horn wrote:

 you could alias it for all your types
 
 alias TFoo(int) foo;
 alias TFoo(float) foo;
 alias TFoo(char) foo;
 
 but then you're back to C++ template instantiation madness
 I agree it makes userland code a bit messy to have the !() everywhere 
 when you wish the compiler would instantiate it for you--
 but it is also a big can of worms with respect to overload resolution 
 and so forth I suspect
 
 Maagar wrote:
 
 Right now D does not support template argument deduction, so its 
 interesting if
 this feature is planned at all?

 In C++ compiler can guess types and automatically instantiate 
 template, it would
 be very nice to have this feature in D.

 example (c++):

 template <typename T> void foo(T arg)
 {
 }
 ..
 f(5);
 f(5.0);
 f('c');

 while in D it would be something like:

 TFoo!(int).f(5);
 TFoo!(float).f(5.0);
 TFoo!(char).f('c');


May 19 2004
prev sibling parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
Daniel Horn wrote:

 I agree it makes userland code a bit messy to have the !() everywhere
 when you wish the compiler would instantiate it for you--
 but it is also a big can of worms with respect to overload resolution
 and so forth I suspect

Personally, I believe that can is worth opening. Expression templates in C++ absolutely depend on this feature, and there are many other places where it is far more than a minor syntax nuissance to type the !() stuff before every call.
May 20 2004
parent hellcatv hotmail.com writes:
the most useful place is clearly operator overloading
because there's no way you can do
a*!(typeof(b)) b 
(that's not valid syntax even)

In article <c8hmdr$29ko$1 digitaldaemon.com>, Norbert Nemec says...
Daniel Horn wrote:

 I agree it makes userland code a bit messy to have the !() everywhere
 when you wish the compiler would instantiate it for you--
 but it is also a big can of worms with respect to overload resolution
 and so forth I suspect

Personally, I believe that can is worth opening. Expression templates in C++ absolutely depend on this feature, and there are many other places where it is far more than a minor syntax nuissance to type the !() stuff before every call.

May 20 2004