Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Template Question

Name: Anonymous 2012-10-07 21:47


template <size_t N>
void print_members(int (&arr)[N]) { ... }


so is C++ going to create a new function for every different sized array I use

Name: Anonymous 2012-10-08 3:50

>>2
the compiler could try to implement the function as:


void print_members(int* arr, int N) { ... }


it would work alright as long as the function was invoked as:


print_members<SOME_CONSTANT>(arr);


But you run into problems as soon as the programmer does:


void (*fn)(int*) = print_members<SOME_CONSTANT>;


because now you have to somehow store the SOME_CONSTANT parameter in the function pointer, which just doesn't work. So the implementation above can't always be used. Basically, every time [code]print_members<SOME_CONSTANT> is referenced but not called, it needs to point to some code that does what is described. But if you are just calling it, then you are free to call a different function with an extra hidden parameter to get the same effect.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List