function-pointers

Why isn't a lambda that captures variables by reference convertible to a function pointer?

你说的曾经没有我的故事 提交于 2019-12-04 08:52:29
If I have a lambda which captures all automatic variables by reference ( [&] {} ), why can't it be converted to a function pointer? A regular function can modify variables just like a lambda that captures everything by reference can, so why is it not the same? I guess in other words, what is the functional difference between a lambda with a & capture list and a regular function such that the lambda is not convertible to a function pointer? So let's take the example of a trivial lambda: Object o; auto foo = [&]{ return o; }; What does the type of foo look like? It might look something like this

pointer to const vs usual pointer (for functions)

佐手、 提交于 2019-12-04 07:49:13
Is there any difference between pointer to const and usual pointer for functions? When it is suitable to use const qualifier for stand alone functions? I wrote short sample to illustrate my question: #include <iostream> using namespace std; int sum( int x, int y ) { return x + y; } typedef int sum_func( int, int ); int main() { const sum_func* sum_func_cptr = ∑ // const function sum_func* sum_func_ptr = ∑ // non-const function ? // What is the difference between sum_func_cptr and sum_func_ptr int x = sum_func_cptr( 2, 2 ); cout << x << endl; int y = sum_func_ptr( 2, 2 ); cout << y << endl; sum

define a function alias in VBA, possible?

∥☆過路亽.° 提交于 2019-12-04 06:13:43
I am using VBA behind MS Access Say I have global methods foo1 and foo2 that gets the same arguments but do different things. I know that in C++ I can assign a function an alias. Something like: instead of: If (term) then foo1 arg1, arg2, arg3 else foo2 arg1, arg2, arg3 End If I want to write: Var new_func = Iff (term, foo1,foo2) new_func arg1, arg2, arg3 Can I do this on vba? Would Run suit? new_func = IIf(term, "foo1", "foo2") ''new_func arg1, arg2, arg3 res = Run(new_func, "a", "b", 1) More information: http://msdn.microsoft.com/en-us/library/aa199108(office.10).aspx Alex K. If the 2

Class member function as function pointer

余生长醉 提交于 2019-12-04 06:00:53
问题 I have a class and one of its member functions is actually a function pointer. That way the user can overwrite what does this function do. I unfortunately have some difficulties running this function. I use g++ to compile. Below is the code: #include <iostream> using namespace std; double fcn_mod(const double &phit){ return 1.2+phit; } class Object{ public: Object() { fcn_ptr = (double (*)(const double &)) &Object::fcn_default; } double (*fcn_ptr)(const double &) = NULL; private: double fcn

typedef'ing function *signature* (not a pointer to), so that it can be reused?

人盡茶涼 提交于 2019-12-04 05:39:27
EDIT: MOTIVATION Suppose I define a Handler class as class Handler { public: class Message { /*...*/ }; typedef int (*Callback)(Message *msg); void registerCallback(int msgclass, Callback f); }; A client can do int f1(Handler::Message *msg) { /* handle message */ } int f2(Handler::Message *msg) { /* handle message */ } int main(){ Handler h; h.registerCallback(1, f1); h.registerCallback(2, f2); // .... } The compiler will indeed check that f1 and f2 are appropriate as parameters to registerCallback , however, it's up to the client to define f1 and f2 correctly. Since I've allready typedef ed

C++ compilation error when passing a function into remove_if

元气小坏坏 提交于 2019-12-04 05:35:19
So here's a snippet of my code. void RoutingProtocolImpl::removeAllInfinity() { dv.erase(std::remove_if(dv.begin(), dv.end(), hasInfCost), dv.end()); } bool RoutingProtocolImpl::hasInfCost(RoutingProtocolImpl::dv_entry *entry) { if (entry->link_cost == INFINITY_COST) { free(entry); return true; } else { return false; } } I'm getting the following error when compiling: RoutingProtocolImpl.cc:368: error: argument of type bool (RoutingProtocolImpl::)(RoutingProtocolImpl::dv_entry*)' does not match bool (RoutingProtocolImpl:: )(RoutingProtocolImpl::dv_entry )' The problem is that this: bool

Why cast “extern puts” to a function pointer “(void(*)(char*))&puts”?

限于喜欢 提交于 2019-12-04 05:14:58
I'm looking at example abo3.c from Insecure Programming and I'm not grokking the casting in the example below. Could someone enlighten me? int main(int argv,char **argc) { extern system,puts; void (*fn)(char*)=(void(*)(char*))&system; char buf[256]; fn=(void(*)(char*))&puts; strcpy(buf,argc[1]); fn(argc[2]); exit(1); } So - what's with the casting for system and puts? They both return an int so why cast it to void? I'd really appreciate an explanation of the whole program to put it in perspective. [EDIT] Thank you both for your input! Jonathan Leffler , there is actually a reason for the code

Why can't I assign python's print to a variable?

[亡魂溺海] 提交于 2019-12-04 04:29:55
I'm learning to program and I am using Python to start. In there, I see that I can do something like this: >>>> def myFunction(): return 1 >>>> test = myFunction >>>> test() 1 However, if I try and do the same with print it fails: >>>> test2 = print File "<stdin>", line 1 test2 = print ^ SyntaxError: invalid syntax Why is print different than a function I create? This is using Python v2.7.5. BrenBarn print is a statement , not a function. This was changed in Python 3 partly to allow you to do things like this. In Python 2.7 you can get print as a function by doing from __future__ import print

Function pointers working as closures in C++

社会主义新天地 提交于 2019-12-04 03:52:07
问题 Is there a way in C++ to effectively create a closure which will be a function pointer? I am using the Gnu Scientific Library and I have to create a gsl_function. This function needs to effectively "close" a couple of parameters available when I create it. Is there a nice trick to create a closure so that I don't have to pass all of them as params in the gsl_function structure? If not, should I just pass in a pointer to an array containing these parameters? EDIT I have tried to use boost:

Call a raw address from Rust

倖福魔咒の 提交于 2019-12-04 03:28:40
问题 I am writing an OS in Rust and need to directly call into a virtual address that I'm calculating (of type u32 ). I expected this to be relatively simple: let code = virtual_address as (extern "C" fn ()); (code)(); However, this complains that the cast is non-primitive. It suggests I use the From trait, but I don't see how this could help (although I am relatively new to Rust and so could be missing something). error[E0605]: non-primitive cast: `u32` as `extern "C" fn()` --> src/main.rs:3:16 |