I encountered a problem basically can be explain by the code below:
void (*fn_ptr)();
template<typename T> void Second(){
//do something
}
template<typename T> void First() {
//do init
fn_ptr = Second<T>;
}
The function First and function Second both need to be template function and will only be called through function pointer fn_ptr. Function First must be called once before function Second is called. You don’t need manually call function Second. So the only thing we should do before fun_ptr is used somewhere is this:
fn_ptr = First<SomeType>;
There comes the question: How can I keep people from doing this:
fn_ptr = Second<SomeType>;
ps: I know how to do it when function First and function Second are not template function
Finally I managed to get it work. Just make the two functions to be static member functions of a class template:
and then:
All I want to say is holly shit.
fn_ptrworks well after functionFirstassign a private static member function to it. Well, it works on VC++ 2012. I don’t know if it comply with the iso standard or not but I guess it’s portable.