   Compile time check
  Just doing some practice and trying to make a compile time isBinarySequencechecker. A binary sequence is any number that only consists of 0's and 1's.This is what I have, but it works for some but not all. I think my problem iswith the partial specialization. Any hints would be nice. template<int binarySequnece, bool isIt = binarySequnece % 10 <= 1>struct isBinarySequence{enum{ result = isBinarySequence<binarySequnece/10, isIt >::result };};template<>struct isBinarySequence<1,true>{enum{ result = 1 };};template<>struct isBinarySequence<0,true>{enum {result = 1 };};template<int binarySequnece>struct isBinarySequence<binarySequnece,false>{enum { result = 0 };};int main(){using namespace Print;//cout << Binary<1011>::result << endl;println(isBinarySequence<10111>::result); // trueprintln(isBinarySequence<10112>::result); // falseprintln(isBinarySequence<10121>::result); // true}Thanks for looking at this post.   
bool isBinary(unsigned int n) {  if (n == 0) return true;  return (n % 10 < 2) && isBinary(n / 10);};;  Thanks for the hint. This is what I came up with. Its the only part of the program that has changed.template<int binarySequnece, bool isIt = (binarySequnece % 10 < 2)  >struct isBinarySequence{enum{ result = isBinarySequence<binarySequnece/10, isIt && isBinarySequence<binarySequnece/10>::result >::result };};It works, as far as I can tell, but I am not sure why it does exactly. AlsoI am not sure why the 1st attempt failed. Can someone explain please?   ;Quote:Original post by ConcentrateIt works, as far as I can tell, but I am not sure why it does exactly. AlsoI am not sure why the 1st attempt failed. Can someone explain please?BS<10121, 10121 % 10 < 2 >BS<1012, 10121 % 10 < 2 >BS<101, 10121 % 10 < 2 >BS<10, 10121 % 10 < 2 >BS<1, 10121 % 10 < 2 >See the problem? isIt gets assigned only once, at the beginning, then the value is propagated through call chain.Hence my original solution:template < int bs > struct BS    { enum { result = (bs % 10 < 2) && BS<bs/10>::result; };template <   > struct BS<0> { enum { result = 1; } };Or:result = isBinarySequence<binarySequnece/10, binarySequnece % 10 < 2>::result // orresult = isBinarySequence<binarySequnece/10>::resultin original code.   ;  Oh when when I do this :template<int binarySequnece, bool isIt = (binarySequnece % 10 < 2)  >struct isBinarySequence{enum{ result = isBinarySequence<binarySequnece/10, isIt >::result };};the isIt gets evaluated only once, specifically at the start of the program?how come the second call to isBinarySequence does not evaluate the isIt withthe new binarySequence? That confuses me.Also as I found out this code has a very subtle bugtemplate<int binarySequnece, bool isIt = binarySequnece % 10 < 2  >struct isBinarySequence{enum{ result = isBinarySequence<binarySequnece/10, isIt && isBinarySequence<binarySequnece/10>::result >::result };};The bug is in the "template<int binarySequnece, bool isIt = binarySequnece % 10 < 2  >Microsoft wont compile this because the compiler things "<" before the 2 and the ">" after the two are matching angle brackets. So I have to put parenthesisaround the (binarySequence % 10 < 2 ) for it to work. Its a bug, at least forvisual studio 2008 express.;Quote:Original post by Concentratethe isIt gets evaluated only once, specifically at the start of the program?how come the second call to isBinarySequence does not evaluate the isIt withthe new binarySequence? That confuses me.void foo(int n, int m = 10) {  if (n == 0) return;  foo(n-1, m);}foo(10);Put printfs in there or use a debugger.Quote:Also as I found out this code has a very subtle bugIt's not a bug, it's syntactic ambiguity in language definition.   ;Quote:Original post by AntheusQuote:Original post by Concentratethe isIt gets evaluated only once, specifically at the start of the program?how come the second call to isBinarySequence does not evaluate the isIt withthe new binarySequence? That confuses me.void foo(int n, int m = 10) {  if (n == 0) return;  foo(n-1, m);}foo(10);Put printfs in there or use a debugger.Quote:Also as I found out this code has a very subtle bugIt's not a bug, it's syntactic ambiguity in language definition.Oh I see. Thanks so much for your help. You saved me hours.   