Topic: Problems with MS-VC++6 templates
Author: fzhong@my-deja.com
Date: 2000/01/23 Raw View
hi, I tried your code in gcc295 without any problem. I think
VC++6 does not support template partial specialization.
Frank Zhong
Sent via Deja.com http://www.deja.com/
Before you buy.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Fei Li <fli@htc-tech.com>
Date: 2000/01/20 Raw View
Hi Experts:
In my work, I often need to dynamically allocate multi-dimensional
arrays. For example,
int **p;
p = new int*[5];
for( int i = 0; i < 5; ++i)
p[i] = new int[100];
In one of my recent programs, I needed a " T ****p " (sorry for this
ugly creature, there is reason for having it, but it is not the point
of this posting). Tired of writing for-loops to do the allocation, I
cooked up some template functions to do the job. They are listed here:
----------------------------------------------
template<class T> inline void allocate_marray(T &p,size_t *s) {}
template<class T> inline void free_marray(T &p, size_t *s) {}
template<class T>
inline void allocate_marray(T * &p, size_t *sarray) {
p = new T [ sarray[0] ];
for( size_t i = 0; i < sarray[0]; ++i)
allocate_marray(p[i],sarray+1);
}
template<class T>
inline void free_marray(T * &p, size_t *sarray) {
for( size_t i = 0; i < sarray[0]; ++i)
free_marray(p[i],sarray+1);
delete[] p;
}
--------------------------------------------------
in my program I do this:
double ****p;
size_t dims[] = {5,5,5,1000};
allocate_marray(p,dims);
.............
free_marray(p,dims);
When "allocate_marray(T *&p, ....)" is called the first time, the"T"
in the template function is "double***";
the second time (recursive call), "T" is "double**"; the 3rd time,
"T" is "double *"; the 4th time , "T" is "double"
the 5th time, "allocate_marray( T &p, ....)" is called, which does
nothing and hence terminates the recursion.
This works perfectly fine with the Digital C++ compiler I have on a
Dec-alpha work station, but fails to compile
with MS-VC++6. This is not the first time I have trouble with VC++.
My question is: am I doing something wrong here or is the VC++
compiler simply not very smart?
**************************************************************
* Fei Li, | (757)-865-0818 *
* High Technology Corp. | fli@htc-tech.com *
* 28 Research Drive | http://www.htc-tech.com/home/fli *
* Hampton, VA23666 | *
**************************************************************
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]