function-pointers

Reference to Lua function in C/C++

你离开我真会死。 提交于 2019-11-29 17:24:25
问题 I have a functions nested relatively deeply in a set of tables. Is there a way in C/C++ to get a "reference" to that function and push that (and args) onto the stack when I need to use it? 回答1: This is what the reference system is for. The function call r = luaL_ref(L, LUA_REGISTRYINDEX) stores the value on the top of the stack in the registry and returns an integer that can be stored on the C side and used to retrieve the value with the function call lua_rawgeti(L, LUA_REGISTRYINDEX, r) .

How to call functions by their pointers passing multiple arguments in C?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 17:11:48
I need to make a "function caller" function: it receives a generic function pointer ( void * ) and a variable number of arguments as arguments and it's got to call this function, passing the arguments, and return a generic pointer to the returning value. However, this entry-function pointer may point to any kind of function (with any returning type), even to functions with a constant number of arguments. It would be something like: void * function_caller(void * function_pointer, ...) { void * returning_value; // Call the function and get the returning value return returning_value; // this

Cannot move out of captured outer variable in an `Fn` closure

ぃ、小莉子 提交于 2019-11-29 16:48:49
问题 I'm trying to figure out how to send a function through a channel, and how to avoid extra cloning in order to execute the function at the other end. If I remove the extra cloning operation inside the closure, I get the following error: error: cannot move out of captured outer variable in an 'Fn' closure Ignoring the fact that this code does absolutely nothing, and makes use of a global mutable static Sender<T> , it represents what I'm trying to achieve while giving the proper compiler errors.

How come pointer to a function be called without dereferencing?

佐手、 提交于 2019-11-29 16:27:09
I have a weird typedef statement in a C++ program, generated by Py++. double radius(int); // function to be wrapped typedef double (*radius_function_type)(int); bp::def("radius", radius_function_type(&radius)); // bp::def is a function for wrapping What I figured out so far is that the above typedef statemnt is not of the type, most of us are familiar with, typedef complex_type simple_alias; Rather it is a way to declare pointer to a function which takes int as argument and returns double (same as the prototype). So my question now is that, how come pointer to a function (without dereferencing

Why is 'X x; x();' allowed, when 'X' defines a conversion to function pointer, but not, when it defines a conversion to a functor?

余生颓废 提交于 2019-11-29 16:02:01
问题 void f(int){} typedef void (*f_ptr)(int); struct Functor{ void operator()(int){} }; struct X{ operator f_ptr(){ return f; } }; struct Y{ operator Functor(){ return Functor(); } }; int main(){ X x; Y y; x(5); // works ?! y(5); // doesn't ?! } Live example on Ideone. Output: error: no match for call to '(Y) (int)' Q1: Why is the call to x(5) allowed, even though X only defines a conversion to function pointer, and not operator() ? Q2: Conversely, why is the same thing not allowed, if we define

How to pass a pointer to a member function to a C function? [duplicate]

。_饼干妹妹 提交于 2019-11-29 15:15:11
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Using a C++ class member function as a C callback function I'm writing an object-oriented library using a C library (winpcap). I need to pass the callback function that is called when a network packet arrives as a function pointer. I would like to pass a member function pointer to winpcap, to keep my design object oriented and to allow for different objects to receive different packets. However member functions

How to compare 2 functions in Go?

对着背影说爱祢 提交于 2019-11-29 15:08:06
For example I have list of functions that I want to compare: http://play.golang.org/p/_rCys6rynf type Action func(foo string) type Handler struct { Get Action Post Action } var routes map[string]Handler func Undefined(foo string) { } func Defined(foo string) { } func init() { routes = map[string]Handler{ `/`: Handler{Defined,Undefined}, } } func main() { for _, handler := range routes { if handler.Post != Undefined { // do something } // invalid operation: (func(string))(handler.Post) != Undefined (func can only be compared to nil) if &handler.Post != &Undefined { // do something } // cannot

Pointer to current function

纵饮孤独 提交于 2019-11-29 14:40:24
Is there any way to get a pointer to the current function, maybe through gcc extensions or some other trickery? Edit I'm curious whether it is possible to get the function pointer without ever explicitly using the function's name. I thought I had a good reason for wanting this, realized that I didn't really, but am still curious if it is possible. This isn't especially portable, but should work on at least some platforms (i.e., Linux and OSX, where I can check the documentation; it definitely doesn't work on Windows which lacks the API): #include <dlfcn.h> // ... void *handle = dlopen(NULL,

Python ctypes to return an array of function pointers

北慕城南 提交于 2019-11-29 14:22:33
I am working with a .dll that contains a single call which returns an array of function pointers. GetMyApi() returns a pointer to a struct, which is an array of function pointers. The functions themselves have different individual inputs and outputs. What I have tried so far: C code that I can't easily alter: Code in C : typedef struct My_Api_V2 { int (__cdecl *IsValidInt)(int i); int (__cdecl *InvalidInt)(); int (__cdecl *IsValidSize)(size_t i); } my_Api_V2; const my_Api_V2* GetMyApi(int version); // This function is accessed from DLL Python effort: from ctypes import * my_dll = cdll

Why must I use address-of operator to get a pointer to a member function?

落爺英雄遲暮 提交于 2019-11-29 14:06:41
struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } Why must I use address-of operator to get a pointer to a member function? auto p1 = &f; // ok auto p2 = f; // ok The first is more or less the right thing. But because non-member functions have implicit conversions to pointers, the & isn't necessary. C++ makes that conversion, same applies to static member functions. To quote from cppreference : An lvalue of function