function-pointers

Pass a function template to other function

半腔热情 提交于 2019-12-10 15:14:59
问题 Suppose I have a function that does something on an arbitrary container type (C++11): template<class containerType> void bar( containerType& vec ) { for (auto i: vec) { std::cout << i << ", "; } std::cout << '\n'; } I can call this function from another function like this: void foo() { std::vector<int> vec = { 1, 2, 3 }; bar(vec); } Now suppose I have different functions just like bar, and I want to pass one of these functions to foo, then foo would look something like this: template<class

How can a parameter have type but no name?

浪子不回头ぞ 提交于 2019-12-10 15:10:41
问题 I saw a question that got marked as a dupe, but one part of the question did not get answered by the dupe, and I did not find a suitable dupe to correct it. So here goes. I once saw a declaration like this: int (*function)(int, float); I don't really understand it. It takes two arguments, but they have no name. How does that work? I mean, when declaring a function like this: int f(int x, int y) { return x+y; } How would that even be possible without identifiers? I have noticed that this don't

function pointers and return type conversions

余生颓废 提交于 2019-12-10 15:09:01
问题 Suppose I have a function that performs some side effect and then returns an answer: int foo() { perform_some_side_effect(); return 42; } I want to bind foo to a function pointer, but I'm not interested in the answer, just the side effect: void (*bar)() = foo; However, this appears to be a type error: error: invalid conversion from ‘int (*)()’ to ‘void (*)()’ What is the rationale behind that error? Why doesn't the type system allow me to ignore the answer? On a side note, it works if I wrap

SWIG C function pointer and JAVA

久未见 提交于 2019-12-10 14:40:05
问题 I have some code in C and one of the method has a function pointer as argument. I'm trying to use the C code in my Android app. I decided to use SWIG to do all the work in generating the java files I need. Everything works well with regular function (the one that doesn't have a function pointer as argument). But I'm not sure how I can pass my JAVA method as a callback to the C function. Here is an example : here is my multiply.h file typedef int (*callback_t) (int a, int b, int c); int foo

pass lambda expression as member function pointer in c++

佐手、 提交于 2019-12-10 14:35:06
问题 I have a framework function which expects an object and a member function pointer (callback), like this: do_some_work(Object* optr, void (Object::*fptr)()); // will call (optr->*fptr)() How can I pass a lambda expression to it? Want to do somethink like this: class MyObject : public Object { void mystuff() { do_some_work(this, [](){ /* this lambda I want to pass */ }); } }; The meaning of it all is to not clutter the interface of MyObject class with callbacks. UPD I can improve do_some_work

Function Pointer Memory Explanation in C

早过忘川 提交于 2019-12-10 14:28:28
问题 #include <stdio.h> #include <stdlib.h> int (*fptr1)(int); int square(int num){ return num*num; } void main(){ fptr1 = &square; printf("%d\n",fptr1(5)); } Can someone briefly explain what happens in stack when we call a function pointer? What is the difference between calling a function directly in main() and calling it by function pointer in C language by the means of physical memory and process? I tried to understand what happens in memory when we call a function with function pointer but it

C++11: pointers to member function using std::function::target()

烈酒焚心 提交于 2019-12-10 14:04:28
问题 I know this is already a long discussed topic, but I couldn't yet find an answer that satisfies me. Question in short: even using the C++11's function::target() capabilities, is it not possible to pass member function pointers to c-style methods? The following code will not work: the invocation to mbf.target() will return 0 and thus a SEGFAULT is produced. And I don't understand why, because I bind the member function to a generic function object, so the type should be fine. What am I doing

Initializing a function pointer in C

帅比萌擦擦* 提交于 2019-12-10 13:43:57
问题 I have the function uint8_t Authorization_getRole (char const* userId, UsertoRole_T const *roleTable) and in the main program I have: given_Role = Authorization_getRole (userId, roleTable) I want to replace the function call with a function pointer: uint8_t (*getRole_ptr)() given_Role = &getRole_ptr; My questions are: Where do I initalize the function pointer getRole_ptr? How do I initialize the function pointer? Is the syntax below correct? getRole_ptr = Authorization_getRole (userId,

Pass delegates to external C functions in D

馋奶兔 提交于 2019-12-10 13:38:46
问题 How do I pass a delegate to an external C function taking a function pointer, in D? 回答1: Let me cross post what I said on the newsgroup: How do I pass a delegate to an external C function taking a function pointer? You can't do it directly in general, unless you can modify the C function, then you can hack around it, but a delegate and a regular function pointer are pretty different animals. But perhaps you can magic hack it. Observe: // a C function that needs a plain function extern(C) void

g++ -Waddress may misunderstand my meaning

两盒软妹~` 提交于 2019-12-10 10:57:38
问题 The example code is: #include <iostream> using std::cout; using std::endl; void bar(double *) { cout << "call bar()" << endl; } using Bar = void(*)(double *); template <Bar pfunction> void foo() { // when call "foo<bar>()", there is a warning: // the address of ‘void bar(double*)’ will never be NULL [-Waddress] if (nullptr != pfunction) { pfunction(nullptr); } cout << "shit" << endl; } int main() { foo<nullptr>(); // OK foo<bar>(); // warning return 0; } from gcc manual: -Waddress Warn about