function-pointers

std::function as sighandler_t

社会主义新天地 提交于 2019-12-23 23:34:28
问题 How to specify lambda, std::bind result or any other std::function as argument for unix signal function? I'm trying the following std::function<void(int)> handler1 = std::bind(&cancellation_token::cancel, &c); std::function<void(int)> handler2 = [&c](int) { c.cancel(); }; but it doesn't work, because both handler1.target<void(int)>() and handler2.target<void(int)>() return null It works if I initialize handler with free function pointer void foo(int) { ... } std::function<void(int)> handler =

Passing Image Data from Java to C Native function accepting pointers

扶醉桌前 提交于 2019-12-23 20:26:23
问题 I have been provided a C function in an external .so file. This function takes Image data as input and provides the compressed image along with lengh of the image as output. The function template is as follows: char *OutputImage = CompressImage((char *)InputImage, (int)&lenOutImage); In my Java code, I capture the input image as byte[] data type. Now I need to pass this to native C function mentioned above. Can you please help how I can achieve this from Java? 回答1: You have 2 options, use JNI

Using SIMD in a Game Engine Math Library by using function pointers ~ A good idea?

荒凉一梦 提交于 2019-12-23 19:15:18
问题 I have been reading Game Engine Books since I was 14 (At that time I didn't understand a thing:P) Now quite some years later I wanted to start programming the Mathmatical Basis for my Game Engine. I've been thinking long about how to design this 'library'. (Which I mean as "Organized set of files") Every few years new SIMD instructionsets come out, and I wouldn't want them to go to waste. (Tell me if I am wrong about this.) I wanted to at least have the following properties: Making it able to

Clarification on pointer to a function conversion

浪尽此生 提交于 2019-12-23 17:43:56
问题 A function type (lvalue) can be converted to a pointer to function (rvalue). int func(); int (*func_ptr)() = func; But from (4.1/1) An lvalue (3.10) of a non-function, non-array type T can be converted to an rvalue. Does it mean that a lvalue to rvalue conversion is not done on functions? Also, when an array decays to pointer doesn't it return a rvalue which is a pointer? 回答1: Functions are lvalues. A pointer to a function (a data type) can be either; if you give it a name, it's an lvalue;

Uniform random distribution “base class” for both int and double?

人走茶凉 提交于 2019-12-23 17:09:46
问题 I'm trying to make a function that will fill a list with random numbers, and based on the type of the list items it should generate either integer or floating point numbers. So far I've come up with the following code, and it works: template <typename T> void generateRandom(list<T>& numberList){ default_random_engine randomGenerator(random_device{}()); if( typeid(T) == typeid(int) ){ uniform_int_distribution<T> distribution(1000, 2000); auto myGenerator = bind(distribution, randomGenerator);

Inferred return type when passing function by template

五迷三道 提交于 2019-12-23 14:53:38
问题 My question is about having the compiler infer the return type of a function based on the return type of a function passed by template. Is there some way I can call as foo<bar>(7.3) instead of foo<double, int, bar>(7.3) in this example: #include <cstdio> template <class T, class V, V (*func)(T)> V foo(T t) { return func(t); } int bar(double j) { return (int)(j + 1); } int main() { printf("%d\n", foo<double, int, bar>(7.3)); } 回答1: If you want to keep bar as a template argument, I'm afraid you

how to pass member function pointer to std::function

有些话、适合烂在心里 提交于 2019-12-23 12:36:52
问题 How can I pass member function pointer to std::function through a function. I am going to explain it by comparison (Live Test): template<class R, class... FArgs, class... Args> void easy_bind(std::function<R(FArgs...)> f, Args&&... args){ } int main() { fx::easy_bind(&Class::test_function, new Class); return 0; } I get an error message: no matching function for call to ‘easy_bind(void (Class::*)(int, float, std::string), Class*)’ I just don't understand why a function pointer cannot be passed

Stackoverflow and function pointers

被刻印的时光 ゝ 提交于 2019-12-23 12:15:05
问题 I'm quite lost on this one and I hope someone here could help. My application consists of hundreds of functions evaluating numerical code (source is in the 5MB range each) and I manage the functions with a std::map to function pointers. What apparently happens is that I get a stack overflow when trying to pass an argument to one of the functions, accessed by a pointer to it: gdb output: Program received signal SIGSEGV, Segmentation fault. 0x0000000001ec0df7 in xsectiond149 (sme=Cannot access

Function pointer declaration syntax confusion [duplicate]

*爱你&永不变心* 提交于 2019-12-23 12:09:40
问题 This question already has an answer here : What does this invocation of a char array cast as a function do? (1 answer) Closed 4 years ago . I have read and googled about the right-left rule to decode function pointers. For ex: int (*(*fun_one)(char *,double))[9][20]; is: fun_one is pointer to function expecting (char *,double) and returning pointer to array (size 9) of array (size 20) of int. So what is const char code[] = "\x31\xc0"; int main(){ ((void(*)( ))code)(); } " code is ?? returning

how to pass C++ callbacks between unrelated classes?

穿精又带淫゛_ 提交于 2019-12-23 09:53:45
问题 In a non-boost project, I have a class which uses a timer based on a certain user action (button pressed/released). I want this class generic, so it takes callbacks for user defined actions. // TimerClass.h typedef void (*timerCallback)(void); ... Class TimerClass : public CButton { public: void setCallbackShortTime(timerCallback clickFn) { shortCallback = clickFn;} ; void setCallbackLongTime(timerCallback clickFn) { longCallback = clickFn;} ; private: timerCallback shortCallback,