I suspect the OP's interpretation of "pass by reference" was passing a pointer, not the trickery shown by Elkvis (neat as it is, when used right).
Originally Posted by
phantomotap
The code you forward (as Elkvis posted) does not eliminate the size parameter; it moves the size parameter into a template.
That's useful for a convenience function, but it doesn't improve the implementation.
Indeed.
It is also a function that will not work in this example.
Code:
template<int N> void SomeFunction(char (&c)[N]) // Elkvis's function
{
// something with elements of c, cognisant of the value of N
}
void intermediary(char a[])
{
SomeFunction(a); // This will not compile, fortunately
}
int main()
{
char x[2];
SomeFunction(x); // will call Elkvis's template function correctly
intermediary(x); // attempt to call Elkvis's template function
}
Fortunately, a C++ compiler will reward you with a compilation error in the body of intermediary() - unless the SomeFunction() template has been specialised for a char pointer. This is because the argument a inside intermediary() is actually a pointer, lacking size information. Which brings us back to the OP's original problem ....