Distinguish recursive Templates
Timon Gehr via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 22 15:22:46 PDT 2015
On 05/23/2015 12:12 AM, Manfred Nowak wrote:
> Matt Kline wrote:
>
>> isn't making any use of the template argument T
>
> Correct. I do not know how to use `T' to determine the recursion depth of
> the template---and I want no further parameter.
>
> -manfred
>
import std.stdio, std.range, std.algorithm;
template getDepth(T){
static if(is(T==Set!S,S)) enum getDepth=1+getDepth!S;
else enum getDepth=0;
}
class Set(T){
override string toString(){
enum r="Set".repeat.take(getDepth!Set).join;
return r;
}
}
void main(){
auto s0=new Set!uint;
writeln(s0); // writes Set
auto s1=new Set!(Set!uint);
writeln(s1); // writes SetSet
}
More information about the Digitalmars-d-learn
mailing list