8 月
20
2014

C++ Nested Template


#include <iostream>

using namespace std;

template <class T>
struct _pImg {
 int size;
 T* data;
};

typedef _pImg<int> pImg;
typedef _pImg<float> pFloatImg;

template <class T, template <class> class V>
void create(V<T> *img) { // V = _pimg, T = int , float
 img->data = new T[100] { 99 };
 img->size = 100;
}

int main()
{
 pImg x;
 create(&x);

 printf("x.data %d size %d\n", x.data[0], x.size);

 return 0;
}

另一個寫法

#include <iostream>

using namespace std;

#define nested(outer, inner) template <class inner, template <class> class outer>

template <class T>
struct _pImg {
 int size;
 T* data;
};

typedef _pImg<int> pImg;
typedef _pImg<float> pFloatImg;

nested(V, T)
void create(V<T> *img) { // V = _pimg, T = int , float
 img->data = new T[100] { 99 };
 img->size = 100;
}

int main()
{
 pImg x;
 create(&x);

 printf("x.data %d size %d\n", x.data[0], x.size);

 return 0;
}
Written by in: 電腦三兩事 |

尚無留言 »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

Powered by WordPress | Theme: Aeros 2.0 by TheBuckmaker.com