function-pointers

C++: pass function with arbitrary number of parameters as a parameter

不问归期 提交于 2019-11-28 12:09:06
long time browser, first time asker here. I've written a number of scripts for doing various 1D numerical integration methods and compiled them into a library. I would like that library to be as flexible as possible regarding what it is capable of integrating. Here I include an example: a very simple trapezoidal rule example where I pass a pointer to the function to be integrated. // Numerically integrate (*f) from a to b // using the trapezoidal rule. double trap(double (*f)(double), double a, double b) { int N = 10000; double step = (b-a)/N; double s = 0; for (int i=0; i<=N; i++) { double xi

can void* be used to store function pointers? [duplicate]

妖精的绣舞 提交于 2019-11-28 11:58:48
This question already has an answer here: Why are function pointers and data pointers incompatible in C/C++? 14 answers void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())? int send(); void* p = send; Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store all pointers in a single variable? No it may not. According to the C Standard (6.3.2.3 Pointers) 1 A pointer to void may be converted to or from a pointer to any object type . A pointer to any object type may be converted to a pointer

C++ Math Parser with user-defined function

江枫思渺然 提交于 2019-11-28 11:49:38
I want to implement a math parser with user-defined function. There are several problems to be solved. For example, int eg(int a,int b){return a+b;} is the function I want to add to the parser. First: How to store all the functions into a container? std::map<std::string,boost::any> func_map may be a choose (by func_map["eg"]=eg ". However, It's very hard to call the function in this kind of map, for I have to use any_cast<T> to get the real function from the wrapper of boost::any . Second: How to handle the overloaded function? It's true that I can distinguish the overloaded functions by the

How come pointer to a function be called without dereferencing?

♀尐吖头ヾ 提交于 2019-11-28 11:18:29
问题 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

C++ allocates abnormally large amout memory for variables

雨燕双飞 提交于 2019-11-28 11:04:12
I recently got to know an integer takes 4 bytes from the memory. First ran this code, and measured the memory usage: int main() { int *pointer; } It took 144KB. Then I modified the code to allocate 1000 integer variables . int main() { int *pointer; for (int n=0; n < 1000; n++) { pointer = new int ; } } Then it took (168-144=) 24KB but 1000 integers are suppose to occupy (4bytes x 1000=) 3.9KB . Then I decided to make 262,144 integer variables which should consume 1MB of memory . int main() { int *pointer; for (int n=0; n < 262144; n++) { pointer = new int ; } } Surprisingly, now it takes 8MB

C++ function types

南楼画角 提交于 2019-11-28 09:46:59
I have a problem understanding function types (they appear e.g. as the Signature template parameter of a std::function ): typedef int Signature(int); // the signature in question typedef std::function<int(int)> std_fun_1; typedef std::function<Signature> std_fun_2; static_assert(std::is_same<std_fun_1, std_fun_2>::value, "They are the same, cool."); int square(int x) { return x*x; } Signature* pf = square; // pf is a function pointer, easy Signature f; // but what the hell is this? f(42); // this compiles but doesn't link The variable f can not be assigned, but can be called. Weird. What is it

Passing a lambda into a function template

≯℡__Kan透↙ 提交于 2019-11-28 09:41:17
I'm learning C++, and I'm trying to implement a binary search function that finds the first element for which a predicate holds. The function's first argument is a vector and the second argument is a function that evaluates the predicate for a given element. The binary search function looks like this: template <typename T> int binsearch(const std::vector<T> &ts, bool (*predicate)(T)) { ... } This works as expected if used like this: bool gte(int x) { return x >= 5; } int main(int argc, char** argv) { std::vector<int> a = {1, 2, 3}; binsearch(a, gte); return 0; } But if I use a lambda function

Get a pointer to the current function in C (gcc)?

混江龙づ霸主 提交于 2019-11-28 09:12:15
is there a magic variable in gcc holding a pointer to the current function ? I would like to have a kind of table containing for each function pointer a set of information. I know there's a __func__ variable containing the name of the current function as a string but not as a function pointer. This is not to call the function then but just to be used as an index. EDIT Basically what i would like to do is being able to run nested functions just before the execution of the current function (and also capturing the return to perform some things.) Basically, this is like __cyg_profile_func_enter

Templated Function Pointer Arrays in Visual Studio

假装没事ソ 提交于 2019-11-28 08:52:27
问题 Guillaume Racicot gave an excellent answer to this question on how I could specialize template variables. But I'm having trouble in visual-studio-2017 with creating a templated array of function pointers. This code for example: struct vec { double x; double y; double z; }; namespace details { template <typename T> constexpr double X(const T& param) { return param.x; } template <typename T> constexpr double Y(const T& param) { return param.y; } template <typename T> constexpr double Z(const T&

How to compare 2 functions in Go?

China☆狼群 提交于 2019-11-28 08:28:41
问题 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) !=