Topic: Passing array by reference to template function fails!
Author: Niels Dekker <ndekker@REMOVETHISnki.nl>
Date: 1999/08/10 Raw View
After reading the replies to my posting "How to declare C-style array
parameters?" I concluded that it is preferable to pass fixed-size arrays
by reference, whenever possible.
Unfortunately I can't always compile this for template functions:
I tried to implement a function that would set all elements of an array
to zero:
template<unsigned uNrOfElems>
void SetAllElemsZero( int (&raiArray)[uNrOfElems] )
{
unsigned uIndex = 0;
do
{
raiArray[uIndex] = 0;
} while( ++uIndex < uNrOfElems );
}
I would want to call SetAllElemsZero as follows:
{
int aiArray[32];
SetAllElemsZero(aiArray);
}
Isn't this completely legal Standard C++?
Borland 4.5 and 5.0, Watcom 11.0, MSVC 5.0 and 6.0, none of them could
compile it. Does your compiler do it?
The MSVC compilers, for instance, say the following about the function
call:
"error C2783: 'void __cdecl SetAllElemsZero(int (&)[1])' : could not
deduce template argument for 'uNrOfElems'"
I could only get it compiled with an egcs compiler (egcs-1.1b g++).
Another problem:
Suppose I have a template function that produces a message, it stores
the message in an array of 256 characters:
template<class TYPE>
void MyFunc(const TYPE param, char (& rszMessage)[256])
{
if( param == 0 )
{
strcpy(rszMessage, "Parameter is zero!");
}
}
I would call MyFunc as follows:
{
char szMessage[256] = "";
MyFunc(0, szMessage); // Compiler error?
}
Unfortunately Watcom 11.0 gives compiler error for this function call:
Error E651: col(9) cannot instantiate MyFunc
Borland C++ 4.5 also gives compiler error, saying it could not find a
match for 'MyFunc(int,char *)
Are these compilers right to give an error in this case?
Otherwise, do you know if the Watcom compiler has been fixed to accept
the function call? (I haven't tried its patches yet.)
Borland C++ 5.0, MSVC 5.0, MSVC 6.0 and egcs-1.1b g++ compile the call
to MyFunc without complaining.
Thanks very much,
Niels Dekker
ndekker "at" nki.nl
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/08/11 Raw View
On 10 Aug 1999 19:56:36 GMT, Niels Dekker <ndekker@REMOVETHISnki.nl> wrote:
>After reading the replies to my posting "How to declare C-style array
>parameters?" I concluded that it is preferable to pass fixed-size arrays
>by reference, whenever possible.
I prefer passing by pointer or using std::vector whenever possible as
this means less template code.
>Unfortunately I can't always compile this for template functions:
Your code compiles and runs on g++.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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 ]