www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using IFTI to only allow params of a certain template instance?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
Say I have a function which has a parameter whose type has to be an instance 
of a particular template, like so:

class Temp(T)
{
    // blah
}

OK, so the first thing I tried was something like this:

void func(T, U : Temp!(T))(U param)
{

}

but when I call it like

auto t = new Temp!(int)();
func(t);

the compiler doesn't accept it, it says it can't deduce the types.  It looks 
right to me, though.

If I switch the order of the template parameters to func, I get different 
errors (notably, an error saying that I can't have a specialization for a 
deduced parameter).

Is there any way to restrict the parameter like this?  I thought about using 
a static assert with is(), but I can't think of how I'd check that the type 
of the parameter is an instance of Temp. 
Jul 06 2007
parent reply BCS <BCS pathlink.com> writes:
Jarrett Billingsley wrote:
 Say I have a function which has a parameter whose type has to be an instance 
 of a particular template, like so:
 
 class Temp(T)
 {
     // blah
 }
 
 OK, so the first thing I tried was something like this:
 
 void func(T, U : Temp!(T))(U param)
 {
 
 }
 
 but when I call it like
 
 auto t = new Temp!(int)();
 func(t);
 
 the compiler doesn't accept it, it says it can't deduce the types.  It looks 
 right to me, though.
 
 If I switch the order of the template parameters to func, I get different 
 errors (notably, an error saying that I can't have a specialization for a 
 deduced parameter).
 
 Is there any way to restrict the parameter like this?  I thought about using 
 a static assert with is(), but I can't think of how I'd check that the type 
 of the parameter is an instance of Temp. 
 
 
?? void func(T)(Temp!(T) param) ?? class Temp(T) { private alias T t } void func(U)(U.t param) // totaly un tested
Jul 06 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <BCS pathlink.com> wrote in message 
news:f6mhtv$2pi9$1 digitalmars.com...

 void func(T)(Temp!(T) param)
..I don't know why I didn't think of that. Thanks :)
Jul 06 2007