问题描述
假设我们有两个函数 (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.
这篇关于如何在容器中存储具有不同签名的功能对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!