Pass std::function as parameter to const void *


Aria_JC

I have the following function declaration:

void get_data(struct myStruct* const value, const void * const data);

I have another function where I want to add a std::functionas a parameter:

// I think the std::function definition is where my problem is
template<typename MyType>
void process(bool test, std::function<void(MyType* const, const void* const)>& callb) { ... }

But I'm not quite sure how to call it, or if my definition above is correct:

bool test1 = true;
process<myStruct>(test1, get_data);

Compiler error:

error: prototype of 'void process(bool, std::function<void(MyType* const, const void* const)>&)' does not match anything in class 'MyClass'
 void procedure (boolean test,
错误:候选是:template<class MyType> void process(bool, std::function<void(MyType*, const void*)>&)
     void procedure (boolean test,
... continue

Any ideas?

Burdock

You basically just need to remove the reference from the function object:

void process(bool test, std::function<void(MyType* const, const void* const)> callb);

While references to std::functionobjects cannot be related/converted to the underlying function pointer type, non-reference objects can be implicitly converted to function pointer types due to their constructors.

Edit: There is no performance benefit to passing std::functionas a const reference , but may actually contain some penalties in some corner cases, see Should I pass std::function by const reference?

Related


Pass const char instead of std::string as function parameter

Abruzzo Forte and Gentile This is a newbie question, but I don't understand how it works. Suppose I have the following function void foo(const std::string& v) { cout << v << endl; } and the call in my program below. foo("hi!"); Essentially, I'm passing a

Pass a pointer to std::function<void()>

dvrer What is the best practice to avoid UB when passing std::function<void()>in pointers []? Will cls_ptrit always work if the object is not deleted second_on_finish? Here is the current code: void Foo() { SomeClass* cls_ptr = GetPointerToClass(); st

Pass a pointer to std::function<void()>

dvrer What is the best practice to avoid UB when passing std::function<void()>in pointers []? Will cls_ptrit always work if the object is not deleted second_on_finish? Here is the current code: void Foo() { SomeClass* cls_ptr = GetPointerToClass(); st

Pass function returning void * as parameter

John Doe I want to pass a function as a parameter and return void *a to another function. Right now I have the following code: MyClass *MyClass::bind(std::function<void*(void *)> fun) { this->value = fun(this->value); return this; } valueis void *an i

Also pass function as void* and parameter also as void*

Sephora I'm also having trouble passing void*parameters through functions void*: #include <stdlib.h> void* hello(void* (*calc)(void *), void* op){ int val = *(int *) op; int* retval = malloc(sizeof(int)); *retval = calc(val); return retval; }

Pass std::function* to template parameter

Anthony Monterosa Since template parameters can only have pointers to objects, and lambda literals are not allowed, I've been trying to find a way to capture the lambda and pass it as a std::function. Since the parameter can't be a normal function, I know it h

Pass std::function* to template parameter

Anthony Monterosa Since template parameters can only have pointers to objects, and lambda literals are not allowed, I've been trying to find a way to capture the lambda and pass it as a std::function. Since the parameter can't be a normal function, I know it h

Pass void function as parameter with two parameters

Offir Pe'er I want to pass to SetTimer function TagContactByMail instead of 2 strings. public void AddTagToContact(string tagName, string contactEmail) { SetTimer(tagName, contactEmail); aTimer.Dispose(); } Thi

Pass array to callback function in void parameter

traffic red A callback function with this prototype: void serverAnswer(void *pUserData, int flag); I need to pass a buffer to this function const uint8_t *bufwith a void parameter . How do I pass the buffer, and how do I access the elements of the buffer in t

Pass void function as parameter with two parameters

Offir Pe'er I want to pass to SetTimer function TagContactByMail instead of 2 strings. public void AddTagToContact(string tagName, string contactEmail) { SetTimer(tagName, contactEmail); aTimer.Dispose(); } Thi

Use const std::string pointer as function parameter

Kemmer After years of writing Java, I wanted to dive deeper into C++ again. Although I think I can handle it, I don't know if it is handled with the "state of the art". Currently, I'm trying to understand how to handle std::strings that are passed as constant

Use const std::string pointer as function parameter

Kemmer After years of writing Java, I wanted to dive deeper into C++ again. Although I think I can handle it, I don't know if it is handled with the "state of the art". Currently, I'm trying to understand how to handle std::strings that are passed as constant

Why pass string as const string in function parameter

Risen I'm a little confused about an example in the textbook. After the string is created, it will be created as string type. However, when passing the same string to a function, the function parameter is a const string, not a string. Here is part of the code:

Why pass string as const string in function parameter

Risen I'm a little confused about an example in the textbook. When creating a string, create it as type string. However, when the same string is passed to the function, the function parameters are const stringand string. Here is part of the code: int main() {

Why pass string as const string in function parameter

Risen I'm a little confused about an example in the textbook. After the string is created, it will be created as string type. However, when passing the same string to a function, the function parameter is a const string, not a string. Here is part of the code:

Why pass string as const string in function parameter

Risen I'm a little confused about an example in the textbook. After the string is created, it will be created as string type. However, when passing the same string to a function, the function parameter is a const string, not a string. Here is part of the code:

Why pass string as const string in function parameter

Risen I'm a little confused about an example in the textbook. When creating a string, create it as type string. However, when the same string is passed to the function, the function parameters are const stringand string. Here is part of the code: int main() {

Why pass string as const string in function parameter

Risen I'm a little confused about an example in the textbook. After the string is created, it will be created as string type. However, when passing the same string to a function, the function parameter is a const string, not a string. Here is part of the code:

Pass input parameter to std::function in lambda function

Shohreh I have a class that has a public std::functionmember like this: class B { public: B(std::function<void(void)> _func = NULL) : m_function(_func) { } std::function<void()> m_function; }; I have a class Xwith member functions SomeFunction: class

Pass input parameter to std::function in lambda function

Shohreh I have a class that has a public std::functionmember like this: class B { public: B(std::function<void(void)> _func = NULL) : m_function(_func) { } std::function<void()> m_function; }; I have a class Xwith member functions SomeFunction: class

Pass void function as parameter to double function in C++

connor449 I am trying to solve an optimization problem in c++ using the NLopt library. The documentation indicates that the input function to be optimized should look like this: double myfunc(const std::vector<double> &x, std::vector<double> &grad, void *my_fu