function-pointers

Function pointer to different functions with different arguments in C

て烟熏妆下的殇ゞ 提交于 2019-11-30 12:42:49
I have two functions with variable number and types of arguments double my_func_one(double x, double a, double b, double c) { return x + a + b + c } double my_func_two(double x, double p[], double c) { return x + p[0] + p[1] + c } I want to use a pointer to a function to the functions I defined above based on a some condition getting true e.g. if (true == condition_1) pfunc = my_func_one; else if (true == condition_2) pfunc = my_func_two; // The function that will use the function I passed to it swap_function(a, b, pfunc); My question is, for this scenario, Can I at all define a function

How to pass anonymous functions as parameters in Rust?

半世苍凉 提交于 2019-11-30 11:56:26
I've been playing around with Rust the past week. I can't seem to figure out how to pass a function that is defined as a parameter when calling the method, and haven't come across any documentation that shows them being used in that fashion. Is it possible to define a function in the parameter list when calling a function in Rust ? This is what I've tried so far... fn main() { // This works thing_to_do(able_to_pass); // Does not work thing_to_do(fn() { println!("found fn in indent position"); }); // Not the same type thing_to_do(|| { println!("mismatched types: expected `fn()` but found `||`")

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

北慕城南 提交于 2019-11-30 11:20:01
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. This code is not meant to be ran , just compiled. use std::ops::DerefMut; use std::sync::{Arc, Mutex};

Tool to decipher C/C++ function pointer typedefs

蓝咒 提交于 2019-11-30 10:18:24
I remember once seeing a website, which deciphered complex C++ typedefs including function pointers returning function pointers to functions which return an array with … It would turn such definitions into readable English text: »Pointer to function, returning an array of int-pointers, taking a long and a callback function (taking an int) as parameters«. (something along that lines) typedef int * (*f)(long, void (*)(int))[]; Anybody remember that tool/website? Any hints greatly appreciated iceaway I think you are looking for https://cdecl.org/ . You can find the program itself at https:/

Why are function pointers and data pointers incompatible in C/C++?

和自甴很熟 提交于 2019-11-30 09:10:37
问题 I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn't both be simply addresses into main memory and therefore be compatible? 回答1: An architecture doesn't have to store code and data in the same memory. With a Harvard architecture, code and data are stored in completely different memory. Most architectures are Von Neumann architectures with code and data in the same memory but C

How to use varargs in conjunction with function pointers in C on Win64?

久未见 提交于 2019-11-30 08:37:35
问题 Consider the following C program: #include <stdio.h> #include <stdarg.h> typedef void (callptr)(); static void fixed(void *something, double val) { printf("%f\n", val); } static void dynamic(void *something, ...) { va_list args; va_start(args, something); double arg = va_arg(args, double); printf("%f\n", arg); } int main() { double x = 1337.1337; callptr *dynamic_func = (callptr *) &dynamic; dynamic_func(NULL, x); callptr *fixed_func = (callptr *) &fixed; fixed_func(NULL, x); printf("%f\n", x

Does C++11 std::function limit the number of arguments a function pointer can have?

ぐ巨炮叔叔 提交于 2019-11-30 08:33:10
I'm using the Visual Studio 11 beta and I'm curious about a compilation error i'm getting storing a std::function object in my class. typedef std::function<void (int, const char*, int, int, const char*)> MyCallback; In my class I have, MyCallback m_callback; This compiles just fine. If I add one more argument to the list it fails. typedef std::function<void (int, const char*, int, int, const char*, int)> MyCallback; The failure is: >c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(535): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>' 1> with 1> [ 1>

typedef int (*pf) needs explaining

﹥>﹥吖頭↗ 提交于 2019-11-30 08:32:33
Generally, we use typedef to get alternate names for datatypes. For example -- typedef long int li; // li can be used now in place of long int But, what does the below typedef do? typedef int (*pf) (int, int); Yu Hao typedef int (*pf) (int, int); This means that variables declared with the pf type are pointers to a function which takes two int parameters and returns an int . In other words, you can do something like this: #include <stdio.h> typedef int (*pf)(int,int); int addUp (int a, int b) { return a + b; } int main(void) { pf xyzzy = addUp; printf ("%d\n", xyzzy (19, 23)); return 0; }

What is meaning of a pointer to a constant function?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 08:02:39
Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data. Pointers can be defined to point to a function. My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const with function pointers. Here are some questions: What is the meaning of a pointer to a constant function versus a pointer to a non-constant function? Can a function be const? Can a function be non-const (mutable)? What is the proper (safe) syntax for passing a function pointer? Edit 1: Function pointer syntax typedef void (*Function

C++ instantiate template class from DLL

家住魔仙堡 提交于 2019-11-30 07:50:00
问题 I tried to make a DLL that contains: base template class , only with a virtual destructor and no attributes ( I called it MatrixInterface ) a derived class with constructors, destructor, operator= and attributes ( matrix class ) a function that returns a base class pointer to a new derived object: #ifdef BUILD_DLL #define DLL_EXPORT __declspec(dllexport) #else #define DLL_EXPORT __declspec(dllimport) #endif template<class T> MatrixInterface<T> DLL_EXPORT * CreateMatrixInstance(unsigned int n