/***********************************************************************
**file: template.cpp
**Author: Freeking
**Created: Sep 29th 2009
**Description: 用C++中模板function实现一个数字的幂次方
*************************************************************************/
#include <iostream>
using namespace std;
template <class T>
T power(T base, int exponent)
{
T result = base;
if(0 == exponent)
return (T)1;
if(exponent < 0)
return (T)0;
while( --exponent )
result *= base;
return result;
}
int main()
{
int i = power(5,4);
long l = power(1000L,3);
long double d = power( (long double)1e5, 2);
cout << "i= "<< i <<endl;
cout << "l= " << l <<endl;
cout << "d= "<< d <<endl;
return 0;
}