The following code can be compiled and executed by gcc:
#include <stdio.h>
template <typename T>
T& f1(T & t) {
printf("T\n");
return t;
}
extern int a[];
int main()
{
f1(a);
return 0;
}
But produced error when compiled by VC++2010:
1>------ Build started: Project: cpptest, Configuration: Debug Win32 ------
1> proc1.cpp
1> cpptest.cpp
1>c:\work\cpptest\cpptest\cpptest.cpp(18): error C2664: 'f1' : cannot convert parameter 1 from 'int []' to 'int (&)[1]'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The main issue is that the function template parameter is a reference type meaning the array argument cannot decay to a pointer type. What should be the standard behaviour according to C++03? If it is allowed, what should be the type of T, which is now an incomplete array type, inside the function f1()
and its return type?
EDIT: In C++ array passed-by-reference does not decay to a pointer according to this link. Is it true here?