Howto: c++ Function Pointer with default values

Function pointers themselves can’t have default values. You’ll either have to wrap the call via the function pointer in a function that does have default parameters (this could even be a small class that wraps the function pointer and has an operator() with default paremeters), or have different function pointers for the different overloads of … Read more

& operator optional in function pointer assignment

You are correct that the & is optional. Functions, like arrays, can be automatically converted into pointers. It’s neither compiler-specific nor the result of different language standards. From the standard, Section 6.3.2.1, paragraph 4: A function designator is an expression that has function type. Except when it is the operand of the sizeof operator or … Read more

Using an array of device function pointers

function pointers are allowed on Fermi. This is how you could do it: typedef double (*func)(double x); __device__ double func1(double x) { return x+1.0f; } __device__ double func2(double x) { return x+2.0f; } __device__ double func3(double x) { return x+3.0f; } __device__ func pfunc1 = func1; __device__ func pfunc2 = func2; __device__ func pfunc3 = … Read more

Returning function pointer type

int (*getFunc())(int, int) { … } That provides the declaration you requested. Additionally, as ola1olsson notes, it would be good to insert void: int (*getFunc(void))(int, int) { … } This says that getFunc may not take any parameters, which can help avoid errors such as somebody inadvertently writing getFunc(x, y) instead of getFunc()(x, y).

branch prediction on a function pointer

From The microarchitecture of Intel, AMD and VIA CPUs An optimization guide for assembly programmers and compiler makers http://www.agner.org/optimize/microarchitecture.pdf section 3.7 (for Sandy Bridge, other processors are in other sections) Pattern recognition for indirect jumps and calls Indirect jumps and indirect calls (but not returns) are predicted using the same two-level predictor as branch instructions. … Read more

CUDA function pointers

To get rid of your compile error, you’ll have to use -gencode arch=compute_20,code=sm_20 as a compiler argument when compiling your code. But then you’ll likely have some runtime problems: Taken from the CUDA Programming Guide http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#functions Function pointers to __global__ functions are supported in host code, but not in device code. Function pointers to __device__ … Read more

What’s the best way to cast a function pointer from one type to another?

I think C/C++ are lacking a generic function pointer type, like void* as a generic object pointer type. Generally, converting from one function pointer into another is supported, provided that you don’t call the wrong function pointer type. See [expr.reinterpret.cast]/6: A function pointer can be explicitly converted to a function pointer of a different type. … Read more

C++: Calling member function via pointer

You need to tell the compiler which class the foos are coming from (otherwise it thinks they’re functions from global scope): void A::setPtr(int v){ if(v == 1){ _currentPtr = &A::foo1; // ^^^^ } else { _currentPtr = &A::foo2; // ^^^^ } } and you need a set of parentheses here: std::cout << (this->*_currentPtr)(4,5); // ^ … Read more