How to store functional objects with different signatures in a container?(如何在容器中存储具有不同签名的功能对象?)
问题描述
假设我们有两个函数 (void : ( void ) )
和 (std::string : (int, std::string))
并且我们可以有还有 10 个.所有(或其中一些)接受不同的参数类型,并且可以返回不同的类型.我们想将它们存储在 std::map
中,所以我们得到了这样的 API:
So imagine we had 2 functions (void : ( void ) )
and (std::string : (int, std::string))
and we could have 10 more. All (or some of them) take in different argument types and can return different types. We want to store them in a std::map
, so we get an API like this:
//Having a functions like:
int hello_world(std::string name, const int & number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
//and
void i_do_shadowed_stuff()
{
return;
}
//We want to be capable to create a map (or some type with similar API) that would hold our functional objects. like so:
myMap.insert(std::pair<std::string, fun_object>("my_method_hello", hello_world) )
myMap.insert(std::pair<std::string, fun_object>("my_void_method", i_do_shadowed_stuff) )
//And we could call tham with params if needed:
int a = myMap["my_method_hello"]("Tim", 25);
myMap["my_void_method"];
我想知道如何将许多不同的功能放入同一个容器中.具体来说,如何使用 Boost 在 C++03 中做到这一点.
I wonder how to put many different functions into the same container. Specifically, how to do this in C++03 using Boost.
API 应该独立于实际的函数类型(具有 int a = myMap["my_method_hello"]("Tim", 25);
而不是 int a = myMap
).
The API should be independent from the actual function types (having int a = myMap["my_method_hello"]("Tim", 25);
not int a = myMap<int, (std::string, int)>["my_method_hello"]("Tim", 25);
).
推荐答案
#include <functional>
#include <iostream>
#include <string>
#include <map>
class api {
// maps containing the different function pointers
typedef void(*voidfuncptr)();
typedef int(*stringcrintptr)(std::string, const int&);
std::map<std::string, voidfuncptr> voida;
std::map<std::string, stringcrintptr> stringcrint;
public:
// api temp class
// given an api and a name, it converts to a function pointer
// depending on parameters used
class apitemp {
const std::string n;
const api* p;
public:
apitemp(const std::string& name, const api* parent)
: n(name), p(parent) {}
operator voidfuncptr()
{ return p->voida.find(n)->second; }
operator stringcrintptr()
{ return p->stringcrint.find(n)->second; }
};
// insertion of new functions into appropriate maps
void insert(const std::string& name, voidfuncptr ptr)
{ voida[name]=ptr; }
void insert(const std::string& name, stringcrintptr ptr)
{ stringcrint[name]=ptr; }
// operator[] for the name gets halfway to the right function
apitemp operator[](std::string n) const
{ return apitemp(n, this); }
};
用法:
api myMap;
int hello_world(std::string name, const int & number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
int main()
{
myMap.insert("my_method_hello", &hello_world );
int a = myMap["my_method_hello"]("Tim", 25);
}
不是很漂亮.更好的建议是,即使是远程操作,也不要像您正在尝试做的那样做任何事情.
Not very pretty. Better advice is to not do anything even remotely like whatever it is you're trying to do.
请注意,这要求所有具有相同参数的函数返回相同的类型.
Note that this requires all functions with the same parameters to return the same type.
这篇关于如何在容器中存储具有不同签名的功能对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在容器中存储具有不同签名的功能对象?
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01