Consider:
#include <iostream>
template <typename T> T getArray( T &arr ) {
return *arr;
}
int main() {
int a[] = {5, 3, 6};
std::cout << getArray(a);
}
It’s suppose to print the first element in the array but it is not not working. Why is that?
It gives me the error:
error: no matching function for call to 'getArray(int [3])'
The type of
aisint[3], so the type ofTisint[3]. Arrays cannot be returned from functions.In C++11, you can do this:
Or this:
In C++03 you can do this, but it’s not quite the same:
Or: