今天想用模板元编程,网上搜了下,很多用boost::mpl实现,我自己写了个简单测试了下:
#include <iostream>
using namespace std;
template <int N = 0>
struct Test
{
Test():num(N){
}
int num;
int arr[N];
void GetNum()
{
printf("%d\n", num);
}
};
int main()
{
Test<55> test;
test.GetNum();
}int main()
{
int num = 0;
cin >> num;
Test<num> test;
test.GetNum();
}#include <iostream>
using namespace std;
//template <int N = 0>
struct Test
{
Test(int N):num(N){
arr = new int[N];
}
~Test(){
delete []arr;
}
int num;
int *arr;
void GetNum()
{
printf("%d\n", num);
}
};
int main()
{
int num = 0;
cin >> num;
Test test(num);
test.GetNum();
}原文:http://blog.csdn.net/boyxiaolong/article/details/23198233