Thread overview
Using IFTI to only allow params of a certain template instance?
Jul 06, 2007
BCS
July 06, 2007
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.


July 06, 2007
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
July 07, 2007
"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 :)