CUDA C++11, array of lambdas, function by index, not working
<p>I am having trouble trying to make a CUDA program manage an array of lambdas by their index. An example code that reproduces the problem</p>

<pre><code> #include &lt;cuda.h&gt;
 #include &lt;vector&gt;
 #include &lt;stdio.h&gt;
 #include &lt;stdlib.h&gt;
 #include &lt;time.h&gt;
 #include &lt;sys/time.h&gt;
 #include &lt;cassert&gt;

 #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
 inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true){
     if (code != cudaSuccess) {
         fprintf(stderr,"GPUassert: %s %s %d\n",
         cudaGetErrorString(code), file, line);
         if (abort) exit(code);
     }   
 }

 template&lt;typename Lambda&gt;
 __global__ void kernel(Lambda f){ 
     int t = blockIdx.x * blockDim.x + threadIdx.x;
     printf("device: thread %i: ", t); 
     printf("f() = %i\n", f() );
 }

 int main(int argc, char **argv){
     // arguments
     if(argc != 2){ 
         fprintf(stderr, "run as ./prog i\nwhere 'i' is function index");
         exit(EXIT_FAILURE);
     }   
     int i = atoi(argv[1]);


     // lambdas
     auto lam0 = [] __host__ __device__ (){ return 333; };
     auto lam1 = [] __host__ __device__ (){ return 777; };


     // make vector of functions
     std::vector&lt;int(*)()&gt; v;
     v.push_back(lam0);
     v.push_back(lam1);


     // host: calling a function by index
     printf("host: f() = %i\n", (*v[i])() );


     // device: calling a function by index
     kernel&lt;&lt;&lt; 1, 1 &gt;&gt;&gt;( v[i] ); // does not work
     //kernel&lt;&lt;&lt; 1, 1 &gt;&gt;&gt;( lam0 ); // does work
     gpuErrchk( cudaPeekAtLastError() );
     gpuErrchk( cudaDeviceSynchronize() );
     return EXIT_SUCCESS;
 }
</code></pre>

<p>Compiling with</p>

<pre><code>nvcc -arch sm_60 -std=c++11 --expt-extended-lambda main.cu -o prog
</code></pre>

<p>The error I get when running is</p>

<pre><code>➜  cuda-lambda ./prog 0
host: f() = 333
device: GPUassert: invalid program counter main.cu 53
</code></pre>

<p>It seems that CUDA cannot manage the int(*)() function pointer form (while host c++ does work properly). On the other hand, each lambda is managed as a different data type, no matter if they are identical in code and have the same contract. Then, how can we achieve function by index in CUDA?</p>