function-pointers

Comparing std::functions for equality?

梦想与她 提交于 2019-11-28 08:03:53
How can I compare two C++11 std::function s with operator== , and return true if both of said function s refer to the same function pointer? operator== for std::function compares a std::function with a null pointer, as far as I can tell the standard does not provide any details as to why. Although, this boost FAQ entry, Why can't I compare boost::function objects with operator== or operator!=? provides a rationale and as far as I can tell should be applicable to std::function as well. Quoting the FAQ: Comparison between boost::function objects cannot be implemented "well", and therefore will

Do function pointers force an instruction pipeline to clear?

假如想象 提交于 2019-11-28 07:24:53
Modern CPUs have extensive pipelining, that is, they are loading necessary instructions and data long before they actually execute the instruction. Sometimes, the data loaded into the pipeline gets invalidated, and the pipeline must be cleared and reloaded with new data. The time it takes to refill the pipeline can be considerable, and cause a performance slowdown. If I call a function pointer in C, is the pipeline smart enough to realize that the pointer in the pipeline is a function pointer, and that it should follow that pointer for the next instructions? Or will having a function pointer

C++, function pointer to the template function pointer

徘徊边缘 提交于 2019-11-28 06:23:49
I am having a pointer to the common static method class MyClass { private: static double ( *pfunction ) ( const Object *, const Object *); ... }; pointing to the static method class SomeClass { public: static double getA ( const Object *o1, const Object *o2); ... }; Initialization: double ( *MyClass::pfunction ) ( const Object *o1, const Object *o2 ) = &SomeClass::getA; I would like to convert this pointer to the static template function pointer: template <class T> static T ( *pfunction ) ( const Object <T> *, const Object <T> *); //Compile error where: class SomeClass { public: template

Calling a function pointer whose assigned function has less arguments then the pointer type

一个人想着一个人 提交于 2019-11-28 06:21:38
问题 Consider the following code: #include <iostream> typedef int (*test_func_t) (int, int, int); int print_integer (int a) { std::cout << "num: " << a << "\n"; return a; } int main (int argc, char * argv[]) { test_func_t func = (test_func_t) &print_integer; std::cout << "calling with 3 parameters func(5,7,9)\n"; func(5,7,9); return 0; } As you can see, a type (test_func_t) is defined as a function with 3 int arguments. A function pointer (func) is assigned with a pointer to "print_integer", which

C# P/Invoke: Marshalling structures containing function pointers

和自甴很熟 提交于 2019-11-28 06:00:51
Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke internals better than I do. Here is how I'm marshalling structures containing function pointers from C to C#. I would like to know whether it's the cleanest and/or most efficient way of doing it. I'm interfacing with a native DLL coded in C that provides the following entry point: void* getInterface(int id); You have to pass getInterface(int) one of the following enum values: enum INTERFACES { FOO, BAR }; Which returns a pointer to a structure containing function pointers like: typedef struct IFOO {

C to Python via Ctypes - Wrapping Struct of Function Pointers to Static Functions

寵の児 提交于 2019-11-28 05:55:47
问题 I have structs in a C library that are like this. The function pointers in DataFn point to static functions. .h struct Data { int i; int *array; }; typedef struct { bool (* const fn1) (struct Data*, const char *source); .... } DataFn; extern DataFn const DATAFUNC Using objdump, the table only contains DATAFUNC and a few other things from gcc. This is fine in C where calling fn1 would go like DATAFUNC.fn1(..., ...), but how would something like this be wrapped around so fn1 can be called in

Get a pointer to object's member function

坚强是说给别人听的谎言 提交于 2019-11-28 05:49:33
Here is the problem: 1) I have a class like so: class some_class { public: some_type some_value; int some_function(double *a, double *b, int c, int d, void *e); }; 2) Inside some_function , I use some_values from some_class object to get a result. 3) So, I have a concrete object and I want to get a pointer to this object some_function . Is it possible? I can't use some_fcn_ptr because the result of this function depends on the concrete some_value of an object. How can I get a pointer to some_function of an object? Thanks. typedef int (Some_class::*some_fcn_ptr)(double*, double*, int, int, void

Cast member function for create_pthread() call

夙愿已清 提交于 2019-11-28 05:13:34
问题 I want to stop the warning server.cpp:823: warning: converting from 'void* (ClientHandler:: )()' to 'void ( )(void )' in the call: pthread_create(th, NULL, (void* (*)(void*)) &ClientHandler::handle, (void *) clientHandler); where handle() is a member function of ClientHandler : void* ClientHandler::handle(); I have difficulties deciphering the function-type message from the compiler. The question is: Should I change the handle() interface? Can I get rid of casting overall? Should I change the

C++: how to define a class method as a start routine to thread (with pthread library)

邮差的信 提交于 2019-11-28 04:55:12
问题 i have a Base class and Derived class. they have a virtual function- virtual void action() how can i pass it to *pthread_create()* function? example(with errors): class Base{ protected: pthread_t tid; public: virtual void* action() = 0; }; class Derived : public Base{ void* action(); Derived(){ pthread_create(&tid, NULL, &action, NULL); } }; maybe it should be static? i tried lot of combinations but cant find solution.. 回答1: I ran into this problem a couple months back working on my senior

How to call the function using function pointer?

不问归期 提交于 2019-11-28 04:42:03
Suppose I have these three functions: bool A(); bool B(); bool C(); How do I call one of these functions conditionally using a function pointer, and how do I declare the function pointer? Abhineet You can do the following: Suppose you have your A,B & C function as the following: bool A() { ..... } bool B() { ..... } bool C() { ..... } Now at some other function, say at main: int main() { bool (*choice) (); // now if there is if-else statement for making "choice" to // point at a particular function then proceed as following if ( x == 1 ) choice = A; else if ( x == 2 ) choice = B; else choice =