function-pointers

How do I pass an instance member function as callback to std::thread [duplicate]

可紊 提交于 2019-12-06 01:35:29
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Start thread with member function I'm VERY new to C++. My experience has mostly been with javascript and java. I'm using Xcode on Lion. The following code gives me a compilation error "Reference to non-static member function must be called; did you mean to call it with no arguments?" class MyClass { private: void handler() { } public: void handleThings() { std::thread myThread(handler); } }; I also tried this-

Does TCL have some concept of function pointers?

空扰寡人 提交于 2019-12-06 01:26:35
Working with TCL and I'd like to implement something like the Strategy Pattern . I want to pass in the "strategy" for printing output in a TCL function, so I can easily switch between printing to the screen and printing to a log file. What's the best way to do this in TCL? TCL allows you to store the name of a procedure in a variable and then call the procedure using that variable; so proc A { x } { puts $x } set strat A $strat Hello will call the proc A and print out Hello In addition to the answer showing how you assign a procedure to a variable, you can also pass the name of a procedure as

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

蓝咒 提交于 2019-12-06 00:56:09
问题 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

C++ compilation error when passing a function into remove_if

让人想犯罪 __ 提交于 2019-12-06 00:13:46
问题 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

Go: convert unsafe.Pointer to function pointer and vice versa

荒凉一梦 提交于 2019-12-06 00:07:18
In C you can put function pointers into an array of void pointers and convert them back to function pointers of any type: extern int (*fn1)(void); extern void (*fn2)(int); void foo(void) { void *array[2]; int i; /* implicit cast from function pointer to void pointer */ array[0] = fn1; array[1] = fn2; for (i = 0; i < 2; i++) { int (*fp)(int, int, int); /* implicit cast from void pointer to function pointer */ fp = array[i]; /* call function with a different signature */ fp(1, 2, 3); } } I need to do the same in Go, using unsafe.Pointers. The questions are: Can a Go function pointer be converted

Can operators be used as functions? (C++)

我只是一个虾纸丫 提交于 2019-12-05 23:27:43
This is similar to another question I've asked, but, I've created an expression class that works like so: expression<int, int> exp(10, 11, GreaterThan); //expression<typename T, typename U> exp(T val1, U val2, oper op); //where oper is a pointer to bool function(T, U) where GreaterThan is a previously defined function. And I am wondering why I can't do this: expression<int, int> exp(10, 11, >); particularily when > is overloaded as bool operator>(int a, int a){return (a > b);} which is identical to GreaterThan: bool GreaterThan(int a, int b){return (a > b);} A function that returns bool and

Creating a class member pointer function variable that points to a non-static class member function

﹥>﹥吖頭↗ 提交于 2019-12-05 22:56:25
问题 The goal is to have the member variable _AddValue point to the CreateFirstValue function upon class initialization and after the first invocation of AddValue , all future calls to it will invoke CreateAnotherValue . Previously, I just had a single AddValue function with a conditional check to determine which function to call. However, I feel like that implementation is flawed because that if check will occur every time and it seems like a function pointer would be beneficial here. An example:

How compatible are static class methods and regular routine pointers?

会有一股神秘感。 提交于 2019-12-05 22:09:38
It seems to me that static class methods and regular routine pointers are compatible from a practical viewpoint but the compiler doesn't know this. Example: type TFunc = function(i: Integer): string; TMyClass = class public class function StaticMethod(i: Integer): string; static; end; class function TMyClass.StaticMethod(i: Integer): string; begin Result := '>' + IntToStr(i) + '<'; end; function GlobalFunc(i: Integer): string; begin Result := '{' + IntToStr(i) + '}'; end; procedure CallIt(func: TFunc); begin Writeln(func(42)); end; begin CallIt(TMyClass.StaticMethod); // 1a: doesn't compile

Can you cast a “pointer to a function pointer” to void*

主宰稳场 提交于 2019-12-05 21:53:33
Inspired by comments to my answer here . Is this sequence of steps legal in C standard (C11)? Make an array of function pointers Take a pointer to the first entry and cast that pointer to function pointer to void* Perform pointer arithmetic on that void* Cast it back to pointer to function pointer and dereference it. Or equivalently as code: void foo(void) { ... } void bar(void) { ... } typedef void (*voidfunc)(void); voidfunc array[] = {foo, bar}; // Step 1 void *ptr1 = array; // Step 2 void *ptr2 = (char*)ptr1 + sizeof(voidfunc); // Step 3 voidfunc bar_ptr = *(voidfunc*)ptr2; // Step 4 I

avoid specifying redundant template parameters which contain templated function pointer

Deadly 提交于 2019-12-05 19:19:10
Suppose we have this code: template <class T, void (*u)(T&)> void Foo() { // store the function u internally . . . } There are reasons to do something like this and I won't attempt to go into them. However, is there any way to avoid having to specify type T when calling Foo() ? For example, to compile, one normally needs: Foo<int, MyIntFunction>(); But if this int can be deduced from the function pointer, is this possible: Foo<MyIntFunction>(); EDIT I'm aware of the solution to pass the actual function pointer in as a function parameter, however this is not desired here as it has some perf