Templates array detection
    Cube 
    sq at ua.re
       
    Wed Dec 12 06:49:56 PST 2012
    
    
  
Better example code for my other problem. How can I make the 3rd 
foo work on  Datas?
--
struct Data(T)
{
	T elem;
}
void main()
{
	foo(1);
	foo([1,1]);
	auto tmp1 = new Data!(int);
	tmp1.elem = 3;
	foo(tmp1);
	auto tmp2 = new Data!(string);
	tmp2.elem = "hello";
	foo(tmp2);
}
void foo(T)(T t) if(!isArray!T)
{
	writeln(t + 1);
}
void foo(T)(T t) if(isArray!T)
{
	for(int i = 0; i < t.length; i++)
		writeln(t[i]);
}
void foo(T)(T t) if(is(T == Data)) // ?
{
	if(is(T == Data!int))
		writeln(t.elem + 1);
	else if(is(T == Data!string))
		writeln(t.elem);
}
--
    
    
More information about the Digitalmars-d-learn
mailing list