function-pointers

Python function pointers within the same Class

狂风中的少年 提交于 2019-12-23 01:27:17
问题 I have the following class in Python that I am trying to use for calling a set of its own methods through a dictionary that has the pointers of the available functions: class Test(): functions = { 'operation_a' : Test.function_a; 'operation_b' : Test.function_b; } def run_operations(operation, *args, **kwargs): try: functions[str(operation)](self, args, kwargs) except KeyError: // some log ... def function_a(self, *args, **kwargs): print A def function_b(self, *args, **kwargs): print B This

function pointer handling in Doxygen in C

↘锁芯ラ 提交于 2019-12-22 17:58:45
问题 In my code, a vtable containts several function pointer. Doxygen is unable to follow them. I'd like to force it to recognize the possible paths, to produce a complete Call Graph, since now this part is missing. example: typedef Bool(*SPECIAL_FUNC)(KEY key); typedef struct{ int a; int b; SPECIAL_FUNC; }OBJ; Bool add(KEY key); //code... Bool sub(KEY key); //code... 回答1: While this will likely not affect the call graph in doxygen, you can document the functions as related to the structure, and

Can operators be used as functions? (C++)

我是研究僧i 提交于 2019-12-22 11:14:54
问题 This is similar to another question I've asked, but, I've created an expression class that works like so: expression<int, int> exp(10, 11, GreaterThan); //expression<typename T, typename U> exp(T val1, U val2, oper op); //where oper is a pointer to bool function(T, U) where GreaterThan is a previously defined function. And I am wondering why I can't do this: expression<int, int> exp(10, 11, >); particularily when > is overloaded as bool operator>(int a, int a){return (a > b);} which is

Using non-const expression as template parameter

徘徊边缘 提交于 2019-12-22 10:50:54
问题 This is a follow up on How do I get the argument types of a function pointer in a variadic template class? I have this struct to access the arguments of a variadic template: template<typename T> struct function_traits; template<typename R, typename ...Args> struct function_traits<std::function<R(Args...)>> { static const size_t nargs = sizeof...(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; }; }; And I

Set a function pointer to a static address

我们两清 提交于 2019-12-22 10:48:23
问题 I'm injecting a DLL into another process and want to call a function that is in that binary based on it's address (0x54315). How can I actually declare a function, and then set it to this address? #define FUNC 0x54315 void *myFuncPtr; int main() { myFuncPtr = FUNC; // pretty sure this isn't how myFuncPtr(); // call it? } 回答1: The existing answers work, but you don't even need a variable for the function pointer. You can just do: #define myfunc ((void (*)(void))0x54315) and then call it as

avoid specifying redundant template parameters which contain templated function pointer

前提是你 提交于 2019-12-22 10:27:40
问题 Suppose we have this code: template <class T, void (*u)(T&)> void Foo() { // store the function u internally . . . } There are reasons to do something like this and I won't attempt to go into them. However, is there any way to avoid having to specify type T when calling Foo() ? For example, to compile, one normally needs: Foo<int, MyIntFunction>(); But if this int can be deduced from the function pointer, is this possible: Foo<MyIntFunction>(); EDIT I'm aware of the solution to pass the

Go: convert unsafe.Pointer to function pointer and vice versa

↘锁芯ラ 提交于 2019-12-22 09:47:35
问题 In C you can put function pointers into an array of void pointers and convert them back to function pointers of any type: extern int (*fn1)(void); extern void (*fn2)(int); void foo(void) { void *array[2]; int i; /* implicit cast from function pointer to void pointer */ array[0] = fn1; array[1] = fn2; for (i = 0; i < 2; i++) { int (*fp)(int, int, int); /* implicit cast from void pointer to function pointer */ fp = array[i]; /* call function with a different signature */ fp(1, 2, 3); } } I need

How to get return value from a function called which executes in another thread in TBB?

不问归期 提交于 2019-12-22 09:14:27
问题 In the code: #include <tbb/tbb.h> int GetSomething() { int something; // do something return something; } // ... tbb::tbb_thread(GetSomething, NULL); // ... Here GetSomething() was called in another thread via its pointer. But can we get return value from GetSomething() ? How? 回答1: If you are bound C++03 and tbb you have to use Outputarguments, which means that you have to rewrite your function. e.g.: void GetSomething(int* out_ptr); int var = 23; tbb::tbb:thread(GetSomething, &var); // pay

Generic function pointer in C++11

房东的猫 提交于 2019-12-22 08:55:55
问题 I am currently in the process of writing a method execution queue in C++x0. I have implemented and verified the basic queue mechanism but want to amend it with an option to have push() automatically remove all previous calls to a specific method: queue.push(this, &Obj::foo, 1); queue.push(this, &Obj::foo, 2); queue.push(this, &Obj::foo, 3); should be the same as merely calling queue.push(this, &Obj::foo, 3); My code thus far looks like this: Queue.h: #pragma once #include <functional>

Accessing function pointer inside class

孤街醉人 提交于 2019-12-22 07:59:30
问题 I am defining function pointer inside a class and trying to access it through an instance of the class but it shows an error. Here is the code: 1 #include<stdio.h> 2 3 class pointer { 4 public: 5 int (pointer::*funcPtr)(int); 6 pointer() { 7 funcPtr = &pointer::check; 8 } 9 10 11 int check(int a) 12 { 13 return 0; 14 } 15 16 }; 17 18 int main() 19 { 20 pointer *pt=new pointer; 21 return (pt->*funcPtr)(3); 22 } It shows a compile time error: checkPointer.cpp:21:15: error: ‘funcPtr’ was not