#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; }