Function Pointers, part 1 - Defining and Using Pointers to Templated Functions
(Page 4 of 4 )
Let’s try to make this even more interesting and take a look at how we define pointers to templated functions. First we need a templated function to point at:
template T Inc(T var) {
return ++var;
}
Unfortunately the C++ standard doesn’t allow us to make template declarations in the scope of function bodies, so we cannot define a pointer to a templated function like this in main():
template T (*IncPtr)(T) = &Inc;
It is important to understand how compilers instantiate templates as well. They only instantiate templated code when it is needed, and they will not even detect a syntax error if the templated function is never called (a lot of clever and tricky meta template programming makes good use of this compiler behavior). It doesn’t make sense, and hopelessly complicates things for compiler builders, when you try to declare a templated pointer at a point where the compiler is hard at work instantiating code (as in function bodies). When you make a function call, data is going to be moved onto the memory stack, and the compiler needs to know which parameters to use. By trying to template the pointer, we are denying it that very information, and it won’t be able to continue the compilation.
So what is it we can do with the templated function Inc() we have? When we tell the compiler how we are planning to use the function, defining a function pointer to it works fine:
int main(int argc, char *argv[]) {
// Use Inc to increase an integer
int (*incN)(int) = &Inc;
int nres = incN(1);
(void)printf(“result is: %d\n”, nres);
// Use Inc to increase a float
float (*incF)(float) = &Inc;
float fres = incF(1.f);
(void)printf(“result is: %f\n”, fres);
}
When you run this example you will see that it works fine.
So is there no way we can delay the type definition of the function we want to define a function pointer to? Well, there is one location where this is possible: inside a templated function:
template
T Foo(T var) {
T (*incPtr)(T) = &Inc ;
return incPtr(var); }
Adding the following line to our example shows that it works fine:
int foores = Foo (1);
(void)printf(“result is: %d\n”, result);
When you think about it, it actually makes sense, because by the time Foo is instantiated, typename T is known by the compiler, and it has no problem instantiating the function pointer to Inc.
There is much more to function pointers than actually meets the eye. In the next article I will take a look at class member function pointers and place them in the same context I have done with the regular function pointers in this article. Finally, in the third article you will find a callback implementation example, function pointers mixed with calling conventions and an introduction to C++ functors.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |