I've been doing some exercises in C++ and stumbled upon one, it was about creating a simple a templated function iterator that takes an array of any type and another templated function to do some processing in each element in the array, i've completed the exerisce to look like the following:
template <typename T >
void increment(T& t) {
t += 1;
}
template <typename T, typename F>
void iterate(T *arr, size_t size, F f) {
for (size_t i = 0; i < size; i++) f(arr[i]);
}
int main( void ) {
int tab[]= {0,1,2,3,4};
SomeClass tab2[5];
iterate(tab, 5, increment<int>);
iterate(tab2, 5, increment<SomeClass>);
return (0);
}
Those 2 tests in the main function is those which i wrote my self, now the exercise too provide some tests to check the solution, and it's tests almost the same, it uses a simple print()
templated function instead of increment()
template <typename T >
void print(const T& t) {std::cout << t << " "; };
Now the problem here is it uses the following instantiation for templated function iterate()
:
iterate(tab, 5, print);
iterate(tab2, 5, print);
Now according to what my understanding you cannot pass a templated function as argument without instantiation because it's not a regular function, it should be instantiated otherwise the compiler wouldn't know what type to create for the actual function ?? and indeed when i compiled the exercise's tests i got a:
no matching function for call to 'iterate'
so my question is am i correct and this is just a mistake in the exercise or the tests are correct and i'm the one who's missing something ?