I am currently following this tutorial on SDL2, and have come across templates, which are entirely new to me at this point.
template<typename T, typename... Args>
void cleanup(T *t, Args&&... args){
cleanup(t);
cleanup(std::forward<Args>(args)...);
}
template<> inline void cleanup<SDL_Window>(SDL_Window *window){
...
}
...
I don't understand how calling cleanup()
in the cleanup()
function doesn't just create an endless recursion cycle, and instead calls one of the specialized template functions below. Also, I can't make much sense of calling forward()
for a
but not for b
, as for what I've gathered, forward()
should fix any problems when calling cleanup()
with either lvalues and rvalues.
cleanup(T *a, Vars&&... b)
If you passed n
vars to this function, the first one would go tho the first function in the body (which ends. note that there is no inner calls).
And the rest n-1
would go to the second function in the body and this would call a function again but using n-1
vars.
This process would continue until having only two parameters and then both first and second calls would use the second function because it would be the best match.
Hence this is not endless function.