#include <iostream> using namespace std; template <int n> class static_log2N { public: enum {value = static_log2N<n/2>::value+1 }; } ;
template <> class static_log2N<1> { public: enum{value = 0}; };
int main() { cout<<static_log2N<28>::value<<endl; cin.get(); }
进一步扩展,可以写出一个求出底为k,求log(int k)(int N)的整数部分的程序来,如下:
#include <iostream> using namespace std; template <int base,int n> class static_logxN { public: enum {value = static_logxN<base,n/base>::value+1 }; } ;
template <int base> class static_logxN<base,1> { public: enum{value = 0}; };
int main() { cout<<static_logxN<3,28>::value<<endl; cin.get(); }
|