Home >Backend Development >C++ >How to Correctly Store Member Function Pointers in a `std::function` Map?

How to Correctly Store Member Function Pointers in a `std::function` Map?

Linda Hamilton
Linda HamiltonOriginal
2024-12-21 01:00:10469browse

How to Correctly Store Member Function Pointers in a `std::function` Map?

Storing Member Function Pointers in std::function Map for a Single Class

When attempting to store function pointers to member functions of a specific class in a std::function map, it's crucial to understand the requirement for a non-static member function to be called with an object.

In the provided code, the error stems from the incorrect function signature of std::function. Since non-static member functions always pass "this" as an implicit argument, the signature should be modified to include the object as the first parameter.

The corrected code snippet would be:

To further bind a function with parameters, C placeholders or C 11 lambdas can be utilized. Here are examples for these approaches:

Using Placeholders:

Using Lambdas (C 11):

The above is the detailed content of How to Correctly Store Member Function Pointers in a `std::function` Map?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn