QUOTE(Upka @ 29 May, 2008 - 06:41 PM)

Dear Cerolobo, I agree. A copy of the function will be generated for each value, and this is a disadvantage. But I like the template approach better - it is for lazy people. Write once and forget about the second argument.
Dear Perfectly, couldn't you please explain this (&bounds) stuff? It works, and without it, i.e. int GetBounds(double bounds[N]) it does not.
CODE
double bounds6[6];
int n6 = GetBounds(bounds6); /* generates the following error:
Error 1 error C2784: 'int GetBounds(double [N])' : could not deduce template argument for 'double [N]' from 'double [6]'
*/
Well, I believe it's because if you have a template function that in which any of the template arguments are not a part of the prototype, such as in:
CODE
template <typename T, int N>
int func(const T x)
{
return N;
}
You cannot call func(22);, as the parameter N (an int) cannot be inferred upon based on the provided arguments.
What does this have to do with this situation?
The compiler discards the array index unless the array is passed by reference in the type specification.
For example (on GCC):
cpp
void func(double x[20])
{
x[0] = 4;
}
void func2(double (&x)[20])
{
x[0] = 4;
}
int main() {
double y[3];
// This compiles
func(y);
// This does not
func2(y);
return 0;
}
So, since the compiler ignores the size given in that instance, the instantiation is also ambiguous as the N is thrown out in that instance. You could make it work by using:
GetBounds<sizeof(bounds6)/sizeof(bounds6[0])>(bounds6);
Providing the parameter explicitly, and the template parameter can be determined at compile-time. However, this is not useful.