www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function overload with template alias error

reply =?ISO-8859-1?Q?Andr=E9_Stein?= writes:
Hi,

I'm trying to write a template function which takes a templated alias to 
another type:

struct test(T)
{
}

template aliasT(T)
{
     alias test!(T) aliasT;
}

void foo(T)(test!T t) { // works
}

void foo2(T)(aliasT!T t) { // doesn't work
}

int main(string[] args)
{
     test!(int) t;
     aliasT!(int) t2;
     foo(t);
     foo2(t2); // error
     return 0;
}


When foo2(t2) is called which takes an alias to test!T as argument I get 
the following error from dmd:

*(21): Error: template variant.foo2(T) does not match any function 
template declaration
*(21): Error: template variant.foo2(T) cannot deduce template function 
from argument types !()(test!(int))

I thought that aliasT!T and test!T have the same internal types and the 
compiler would be able deduce the template parameters. Am I missing 
something or is this a bug in DMD? This is a reduced test case from a 
piece of code where I tried to write an templated overload to 
std.variant.Algebraic.

Thanks,
André
Dec 24 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/24/2011 11:23 AM, André Stein wrote:
 Hi,

 I'm trying to write a template function which takes a templated alias to
 another type:

 struct test(T)
 {
 }

 template aliasT(T)
 {
 alias test!(T) aliasT;
 }

 void foo(T)(test!T t) { // works
 }

 void foo2(T)(aliasT!T t) { // doesn't work
 }

 int main(string[] args)
 {
 test!(int) t;
 aliasT!(int) t2;
 foo(t);
 foo2(t2); // error
 return 0;
 }


 When foo2(t2) is called which takes an alias to test!T as argument I get
 the following error from dmd:

 *(21): Error: template variant.foo2(T) does not match any function
 template declaration
 *(21): Error: template variant.foo2(T) cannot deduce template function
 from argument types !()(test!(int))

 I thought that aliasT!T and test!T have the same internal types and the
 compiler would be able deduce the template parameters. Am I missing
 something or is this a bug in DMD? This is a reduced test case from a
 piece of code where I tried to write an templated overload to
 std.variant.Algebraic.

 Thanks,
 André

IFTI can only deduce template parameters for symbols that are defined inside the template instantiation because then the symbol remembers the template parameters. IFTI cannot reverse-instantiate templates.
Dec 24 2011