How to send the caller object as parameter to the callback function

Advertisements How can i access a member of a class object from its own callback function? I try to send the caller object as parameter of the callback function and access it from the callback function. In the following code i get an error: invalid use of void expression on this line test.onEvent(onUpdateEvent(test)); One solution… Read More How to send the caller object as parameter to the callback function

How do I pass an std::function as a parameter to an assignment operator?

Advertisements I have recently upgraded to Visual Studio 2022 and am having problems with assignment operators and std::function. We have a class which allows us to call a function when it is destroyed. Note that I don’t want to use a finally block to solve this. See the code below: class CFinallyCallFunction { public: CFinallyCallFunction(const… Read More How do I pass an std::function as a parameter to an assignment operator?

Calling base class method with std::function

Advertisements I used std::function for basic class virtual method and had strange result. Then I call std::function object, derived (not basic) method is called. Please, can you tell me there is the problem? #include <functional> #include <iostream> #include <string> struct A { virtual void username(const std::string& name) { std::cout << "A: username" << name <<… Read More Calling base class method with std::function

A problem passing functions from std as a function parameter

Advertisements I’m trying to figure out how to pass an std function as a parameter. Here is a brief example: #include <cmath> #include <functional> #include <iostream> void bar(const double v, std::function<int(int)> foo) { std::cout << foo(v) << std::endl; } int main() { bar(-23., std::abs); } This code fails to compile with a message no matching… Read More A problem passing functions from std as a function parameter

Why and how is std::function<void()> allowed to be assigned to callables with return types in C++?

Advertisements The following code is legal C++: int m() { return 42; } int main() { std::function<void()> func, func2; func = m; func2 = [&]() -> std::string { return "This does not return void"; }; } By passing void() to std::function‘s template argument list, I assumed that meant func and func2 must be assigned to… Read More Why and how is std::function<void()> allowed to be assigned to callables with return types in C++?

What exactly is std::function<void(int)> doing in this code?

Advertisements I’ve recently come across some code for the inorder traversal of a binary search tree which is as follows: void binary_search_tree::inorder_traversal(bst_node *node, std::function<void(int)> callback) { if (node == nullptr) { return; } inorder_traversal(node->left, callback); callback(node->value); inorder_traversal(node->right, callback); } std::vector<int> binary_search_tree::get_elements_inorder() { std::vector<int> elements; inorder_traversal(root, [&](int node_value) { elements.push_back(node_value); }); return elements; } From my… Read More What exactly is std::function<void(int)> doing in this code?