Home > Backend Development > C++ > How can I store functions with different signatures in a map in C ?

How can I store functions with different signatures in a map in C ?

Patricia Arquette
Release: 2024-11-13 12:32:02
Original
711 people have browsed it

How can I store functions with different signatures in a map in C  ?

Store Functions with Different Signatures in a Map

In C , you can encounter scenarios where you need to store functions with different signatures in a data structure. Suppose you have a map container that accepts a string key and a generic method as its value. To make this possible, consider the following approach:

Type Erasure and Any Type

C provides the std::any type for type erasure. You can use std::any to store a function pointer or a lambda expression in a container without specifying its exact type. However, to invoke the stored function, you need to cast it to the correct type explicitly.

Operator Overloading for Function Invocation

To overcome the explicit casting, you can define a template operator operator() on a custom wrapper class that takes the stored std::any object as a parameter. This operator allows you to invoke the stored function without the need for explicit casting.

Example Implementation

Here's an example implementation of such a wrapper class:

template<typename Ret>
struct AnyCallable
{
    AnyCallable() {}
    template<typename F>
    AnyCallable(F&& fun) : AnyCallable(std::function(std::forward<F>(fun))) {}
    template<typename ... Args>
    AnyCallable(std::function<Ret(Args...)> fun) : m_any(fun) {}
    template<typename ... Args>
    Ret operator()(Args&& ... args) 
    { 
        return std::invoke(std::any_cast<std::function<Ret(Args...)>>(m_any), std::forward<Args>(args)...); 
    }
    std::any m_any;
};
Copy after login

In this implementation, the AnyCallable class wraps the stored std::any object and provides an operator() that invokes the function with the specified arguments.

Sample Usage

With the AnyCallable class, you can create a map that stores functions with different signatures and invoke them dynamically:

void foo(int x, int y) { /* ... */ }
void bar(std::string x, int y, int z) { /* ... */ }

std::map<std::string, AnyCallable<void>> map;
map["foo"] = &foo;
map["bar"] = &bar;
map["foo"](1, 2);
map["bar"]("Hello", 1, 2);
Copy after login

Considerations

Note that when invoking the stored functions in a type-erased manner, you need to pass the correct argument types explicitly. Type mismatches will result in a std::bad_any_cast exception.

The above is the detailed content of How can I store functions with different signatures in a map in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template