首页 > 后端开发 > C++ > 正文

如何在 C 的映射中存储具有不同签名的函数?

Patricia Arquette
发布: 2024-11-13 12:32:02
原创
639 人浏览过

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;
};
登录后复制

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);
登录后复制

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.

以上是如何在 C 的映射中存储具有不同签名的函数?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板