# Template function pointer

**URL:** <https://forums.developer.nvidia.com/t/template-function-pointer/24615>\
**Category:** CUDA Programming and Performance\
**Created:** [October 13, 2011, 1:51pm UTC](https://forums.developer.nvidia.com/t/template-function-pointer/24615 "2011-10-13T13:51:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roydesbois](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@roydesbois](https://forums.developer.nvidia.com/u/roydesbois)\
**Post date:** [October 13, 2011, 1:51pm UTC](https://forums.developer.nvidia.com/t/template-function-pointer/24615/1 "2011-10-13T13:51:41Z")

</div>

Hi all,

Passing a function pointer to a CUDA kernel is described in detailed in the SDK project FunctionPointers.

In that example, the function accepts a few unsigned char and float parameters and returns an unsigned char.

My kernel is a TEMPLATE kernel and I would like to pass a TEMPLATE function to it.

To this end, I have tried the following:

1. Define a template typedef of the type of function I would like to pass as parameter (typedef cannot be templated as-is, it must be wrapped in a struct/class).

```auto
template<class T>

struct Operators

{

    typedef T (*Operator)(T);

};

```

1. Define a **device** template function ‘d\_Inverse’ of type **device** Operators::Operator

```auto
__device__ Operators<T>::Operator d_Inverse = Inverse;

```

where Inverse is a host function with a number of overloaded implementations, e.g.,

```auto
int Inverse(int i){return 1/i;}

float Inverse(float f){return 1/f;}

```

1. Before the kernel launch, called within a templated “extern” function, I copy the address of ‘d\_Inverse’ to a host variable ‘op’.

```auto
Operators<T>::Operator op = NULL;

cudaError_t error = cudaMemcpyFromSymbol((void*)&op, d_Inverse, (size_t)sizeof(Operators<T>::Operator));

```

1. I call my kernel with op as parameter of type

```auto
Operators<T>::Operator.

```

This code mimics the strategy adopted for non-templated functions. Unfortunately it fails to compile. In particular, the call

```auto
Operators<T>::Operator op = NULL;

```

returns a compilation error, whereas the instantiated version of it

```auto
Operators<float>::Operator op = NULL;

```

does not.

Help is greatly appreciated!

Thanks,

Olivier

---

<div class="post-metadata">

**Author:** ![MarkusM](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MarkusM](https://forums.developer.nvidia.com/u/MarkusM)\
**Post date:** [October 14, 2011, 8:31am UTC](https://forums.developer.nvidia.com/t/template-function-pointer/24615/2 "2011-10-14T08:31:27Z")

</div>

A bit more context and the exact wording of the compiler errors could be helpful.  
Nevertheless I suspect it comes down to a simple problem: There are no template function pointers, since there are no template functions per se, only templates for functions. This means you can only take the pointer of an instantiated template function like the mentioned Operators::Operator.

---

<div class="post-metadata">

**Author:** ![roydesbois](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@roydesbois](https://forums.developer.nvidia.com/u/roydesbois)\
**Post date:** [October 14, 2011, 3:11pm UTC](https://forums.developer.nvidia.com/t/template-function-pointer/24615/3 "2011-10-14T15:11:21Z")

</div>

> [@](#):
>
> A bit more context and the exact wording of the compiler errors could be helpful.
> 
> Nevertheless I suspect it comes down to a simple problem: There are no template function pointers, since there are no template functions per se, only templates for functions. This means you can only take the pointer of an instantiated template function like the mentioned Operators::Operator.

Thanks for your answer. Indeed, the code I posted works fine if I compile it for a particular instantiation of the template T. In this case, however, the “extern” function that calls the GPU kernel must be instantiated for all the template types I would like to implement. I wanted to avoid this code redundancy…
