function-pointers

How do you declare an extern “C” function pointer

感情迁移 提交于 2019-12-04 01:57:31
So I have this code: #include "boost_bind.h" #include <math.h> #include <vector> #include <algorithm> double foo(double num, double (*func)(double)) { return 65.4; } int main(int argc, char** argv) { std::vector<double> vec; vec.push_back(5.0); vec.push_back(6.0); std::transform(vec.begin(), vec.end(), vec.begin(), boost::bind(foo, _1, log)); } And receive this error: return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]); .............................................................^ %CXX-E-INCOMPATIBLEPRM, argument of type "double (* __ptr64 )(double) C" is incompatible with

Can a function pointer with a const argument be used as a function pointer with a nonconst argument?

天大地大妈咪最大 提交于 2019-12-04 00:53:55
Perhaps the title isn't clear in itself... I have a function f (provided by some library) that takes as an argument a function pointer of signature void g(int*) , i.e. void f(void (*g)(int*)); However, I would like to use it using a function g (that I defined) with signature void g(const int*) . A priori, I can't see how this can violate any const-correctness, as all the signature of f says is that g will only ever be called with a (non- const ) int* (non- const ), and indeed I can call a void (const int*) function with a non- const int* argument. But GCC complains and says, expected 'void (*)

Why can't one compare a function pointer to a template function without explicit & on function name?

て烟熏妆下的殇ゞ 提交于 2019-12-04 00:00:58
问题 Consider the following code: void func(int) {} template<typename T> void templatedFunc(T) {} int main() { void (*p)(int) = func; bool test1 = p==func; //bool test2 = p==templatedFunc<int>; // compilation error bool test3 = p==&templatedFunc<int>; // but this works } If you uncomment the test2 line and try to compile the code with g++, you'll get the following error: test.cpp: In function ‘int main()’: test.cpp:8:21: error: assuming cast to type ‘void (*)(int)’ from overloaded function [

POSIX restrictions on pointer types in C

自闭症网瘾萝莉.ら 提交于 2019-12-03 23:57:12
问题 Background The POSIX standard adds a lot of library functions and other identifiers to the C language. In the description of the dlsym() function, it says (with my emphasis): SYNOPSIS #include <dlfcn.h> void *dlsym(void *restrict handle, const char *restrict name); DESCRIPTION The dlsym () function shall obtain the address of a symbol (a function identifier or a data object identifier) ... The C standard doesn't guarantee that a function pointer can be converted to a void * , or even that the

How do you declare a const array of function pointers?

核能气质少年 提交于 2019-12-03 23:55:24
问题 Firstly, I've got functions like this. void func1(); void func2(); void func3(); Then I create my typedef for the array: void (*FP)(); If I write a normal array of function pointers, it should be something like this: FP array[3] = {&func1, &func2, &func3}; I want to make it a constant array, using const before "FP", but I've got this error messages: error: cannot convert 'void ( * )()' to 'void ( * const)()' inialization PD: Sorry my bad English. EDIT: x.h typedef void (*FP)(); class x {

Are two function pointers to the same function always equal?

别说谁变了你拦得住时间么 提交于 2019-12-03 23:32:56
Does the C++ standard guarantee that two pointers to a function always compare equal? I understand that this will normally be true for non-inline functions. But if there is an inline function and a pointer to the function is created in two separate compilation units, will the linker merge the two instantiations, or is it allowed to emit duplicate functions? If the answer to the above is "they are equal": Does this still hold if there is a common header with an inline function, and both the main program and a dynamically loaded plugin (shared object/DLL) create a pointer to the function? Simple

What is the cloest thing to a function pointer in java? [duplicate]

半城伤御伤魂 提交于 2019-12-03 21:44:47
This question already has answers here : Closed 8 years ago . Possible Duplicate: What's the nearest substitute for a function pointer in Java? I just have a situation where it would be nice to have the functionality of something similar to function pointers like you might use in c or c++. The best thing I can think of is combining Runnables and launch them in a different thread. Suggestions? Until they add closures to java, the thing seems closest to java.lang.reflect.Method . For example Method method = SomeClass.getMethod("methodName", methodParameterTypes) Then you can pass the method

Wrapping a C Library with Objective-C - Function Pointers

允我心安 提交于 2019-12-03 20:08:31
I'm writing a wrapper around a C library in Objective-C. The library allows me to register callback functions when certain events occur. The register_callback_handler() function takes a function pointer as one of the parameters. My question to you gurus of programming is this: How can I represent an Objective-C method call / selector as a function pointer? Would NSInvocation be something useful in this situation or too high level? Would I be better off just writing a C function that has the method call written inside it, and then pass the pointer to that function? Any help would be great,

How to implement C++ style function pointers in C#?, Without using delegates

点点圈 提交于 2019-12-03 16:26:36
I am learning pointers in C# and was curious if one can use C++ style function pointers in C#. Yes, I know C# has its own equivalent concept for Function Pointers(called as delegates). But I just want to know if the same can be achieved using pointers in C#, without using delegates. If using pointers is completely legal in C#(using unsafe option) and pointer semantics is almost similar to C/C++ then in my opinion one should also be able to use C/C++ style function pointers as well. Please guide me on this. Is it possible? If yes how?, If not then why? Please notice the similarity of pointer

Is it possible to take the address of an ADL function?

亡梦爱人 提交于 2019-12-03 16:12:30
问题 Is it possible to take the address of a function that would be found through ADL? For example: template<class T> void (*get_swap())(T &, T &) { return & _________; // how do I take the address of T's swap() function? } int main() { typedef some_type T; get_swap<T>(); } 回答1: Honestly, I don't know but I tend towards saying that this is not possible. Depending on what you want to achieve I can suggest a workaround. More precisely, if you just need the address of a function that has the same