Passing Tuple Arguments to Tuple

BCS BCS at pathilink.com
Sun Dec 24 22:26:00 PST 2006


Xinok wrote:
> I was experimenting with recursive templates. I successfully created a
> template which could test if a value is prime or not. What I tried to do next
> is create another template which would use a tuple to build a list of primes.
> 
> template IsPrime(uint V, uint M = 2){
> 	static if(V == 0 || V == 1) const bool IsPrime = false;
> 	else static if(M*M <= V){
> 		static if(V%M == 0) const bool IsPrime = false;
> 		else const bool IsPrime = IsPrime!(V, M+1);
> 	}
> 	else const bool IsPrime = true;
> }
> 
> template FindPrime(V...){
> 	static if(V[0] < 2){
> 		alias V[1..length] FindPrime;
> 	}
> 	static if(IsPrime!(V[0])){
> 		alias FindPrime!(V[0]-1, V[0..length]) FindPrime; // Error: 'tuple V is used
> as a type'
> 	}
> 	else{
> 		alias FindPrime!(V[0]-1, V[1..length]) FindPrime;
> 	}
> }
> 
> 
> This error makes no sense to me. What's the problem? Why can't you pass tuples
> to tuples?


I haven't actually played around with your code but this looks like a 
bug[1] I keep running into. try this

  	static if(IsPrime!(V[0])){
		const uint abc = V[0]; ///<<<<<<<
  		alias FindPrime!(abc-1, V[0..length]) FindPrime; 			}

it messes up implicit proposes but it often makes things work. OTOH a 
cast might do the trick but I haven tried that.

[1] I think this is a bug (see my posts in digitalmars.d.announce about 
"spirit in 100 LOC") but I'm, not sure it tectonically is incorrect.



More information about the Digitalmars-d mailing list