一、题目描述
使用c++提供的模板编程,将二进制数转换成十进制
二、代码
template<size_t N>
class BinToDec {
public:
static const int value = BinToDec<N / 10>::value * 2 + N % 10;
};
template<>
class BinToDec<0> {
public:
static const int value = 0;
};
int main()
{
size_t res = BinToDec<110>::value;
cout << res;
return 0;
}
三、代码分析
结果: