Using IFTI to only allow params of a certain template instance?

BCS BCS at pathlink.com
Fri Jul 6 16:15:10 PDT 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


More information about the Digitalmars-d-learn mailing list