function-pointers

Function lookup table with different function prototypes

帅比萌擦擦* 提交于 2019-12-01 14:48:51
What is the best way to call a specified function based on user input except from series of if and strcmp ? For example: p 2 2 -> call func_p(2, 2) a 8 -> call func_a(7) m -> call func_m(void) I know it's easy and elegant to make a lookup table consisting of function pointers with the same prototype but how about different prototypes? I thought about using ... in a prototype but I don't know if it's a good solution. Define all the functions so they take a single array argument. Comment from Barmar Unifying all functions to the same prototype is exactly what one normally does in this case,

Printing function pointer passed as a parameter results in '1' printed on the screen

家住魔仙堡 提交于 2019-12-01 14:46:09
I've been experimenting with function pointers and found the behavior of the following program rather misterious: void foo(int(*p)()) { std::cout << p << std::endl; } int alwaysReturns6() { return 6; } int main() { foo(alwaysReturns6); return 0; } The above code prints the number '1' on the screen. I know I should access the function pointer like this: p() (and then 6 gets printed), but I still don't get what the plain p or *p means when used in the foo function. std::cout << p << std::endl; here an overload of operator<< which accepts a bool is picked up: basic_ostream& operator<<( bool value

Function lookup table with different function prototypes

痞子三分冷 提交于 2019-12-01 13:29:40
问题 What is the best way to call a specified function based on user input except from series of if and strcmp ? For example: p 2 2 -> call func_p(2, 2) a 8 -> call func_a(7) m -> call func_m(void) I know it's easy and elegant to make a lookup table consisting of function pointers with the same prototype but how about different prototypes? I thought about using ... in a prototype but I don't know if it's a good solution. 回答1: Define all the functions so they take a single array argument. Comment

How to make template parameter

梦想的初衷 提交于 2019-12-01 13:10:20
How do I create a metafunction that takes any kind of function pointer? In the code below, how do I get rid of "decltype(&f)" ? template <class FuncType, FuncType functionPointer> void runFunc() { functionPointer(); } runFunc<decltype(&f),f>(); I don't want to have to specify the type of f seperately; the information is already there in f. I'd prefer not to resort to defines to solve this. This is basically the templated function type idiom applied to meta-programming; I don't want to know the type of f, but whatever I get in apparently allows me to call operator() on it. Stuff I've tried:

Clean implementation of function template taking function pointer

我是研究僧i 提交于 2019-12-01 11:25:11
I've managed to implement and test my function wrapper implementation, however, the interface isn't as nice as it should be: template < typename F, F* f > void register_function( const char* name ) { int (*lf) (lua_State *) = function_wrapper< F, f >; register_native_function( lf, name ); } While this works as expected, the usage requires to explicit template parameters: register_function< decltype(hello), &hello >( "hello" ); Obviously the first parameter could be deduced from the first one, so ideally I'd like to just have register_function< &hello >( "hello" ); Is it possible? Is there a

How could I pass std::function as function pointer?

时光怂恿深爱的人放手 提交于 2019-12-01 10:58:19
I am trying to write a class template and internally it use a C function (implementation of BFGS optimization, provided by the R environment) with the following interface: void vmmin(int n, double *x, double *Fmin, optimfn fn, optimgr gr, ... , void *ex, ... ); where fn and gr are function pointers of type typedef double optimfn(int n, double *par, void *ex); and typedef void optimgr(int n, double *par, double *gr, void *ex); respectively. My C++ class template looks like this: template <typename T> class optim { public: // ... void minimize(T& func, arma::vec &dpar, void *ex) { std::function

is_member_function_pointer implementation

邮差的信 提交于 2019-12-01 10:44:48
I am trying to implement my own is_member_function_pointer and I'm having trouble with it. namespace __implementation { // integral_constant template<typename T, T v> struct integral_constant { static constexpr T result = v; typedef integral_constant<T,v> type; constexpr operator T() { return result; } }; // true_type typedef integral_constant<bool, true> true_type; // false_type typedef integral_constant<bool, false> false_type; // remove_const template<typename T> struct remove_const { typedef T type; }; template<typename T> struct remove_const<const T> { typedef T type; }; // remove

Generic binary search tree in C

血红的双手。 提交于 2019-12-01 08:21:45
I have the implemented a binary search tree but I also want to make it generic. The code is the following: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode; and the functions: treeNode* FindMin(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->left) /* Go to the left sub tree to find the min element */ return FindMin(node->left); else return node; } treeNode * Insert(treeNode *node,int data) { if(node==NULL) { treeNode *temp; temp = (treeNode *)malloc(sizeof(treeNode)); temp -> data = data; temp -> left

How to store various types of function pointers together?

故事扮演 提交于 2019-12-01 07:55:39
问题 Normal pointers can be stored using a generic void* . e.g. void* arr[10]; arr[0] = pChar; arr[1] = pINt; arr[2] = pA; Sometime back, I came across a discussion that, void* may not be capable enough to store a function pointer without data-loss in all platforms (say 64-bit and more). I am not sure about this fact though. If that's true, then what is the most portable way to store a collection of function pointers ? [Note: This question doesn't satisfactorily answer this.] Edit : I will be

is_member_function_pointer implementation

匆匆过客 提交于 2019-12-01 07:29:59
问题 I am trying to implement my own is_member_function_pointer and I'm having trouble with it. namespace __implementation { // integral_constant template<typename T, T v> struct integral_constant { static constexpr T result = v; typedef integral_constant<T,v> type; constexpr operator T() { return result; } }; // true_type typedef integral_constant<bool, true> true_type; // false_type typedef integral_constant<bool, false> false_type; // remove_const template<typename T> struct remove_const {