我有像 this 这样的简单类(class)(其行为类似于 Boost::overload,但其中包含命名函数(我将其用于简化\缩小反射\内省(introspection)目的))。输入参数的多种返回类型存在问题(编译器错误 3066)。我的代码:
#include <iostream>
#include <string>
#include <map>
#include <vector>
template < class T0, class T1 >
class my_map {
typedef T0 type_0;
typedef T1 type_1;
std::map < std::string, type_0 * >T0_var;
std::map < std::string, type_1 * >T1_var;
friend class apitemp;
public:
my_map(int meaningless0 = 42, int meaningless1 = 42) {}
class apitemp {
std::string n_;
my_map *p;
public:
apitemp(std::string name_, my_map * parent):n_(name_), p(parent) {}
operator type_0 *() {
return p->T0_var[n_];
}
operator type_1 *() {
return p->T1_var[n_];
}
};
void insert(std::string name, type_0 * ptr) {
T0_var[name] = ptr;
}
void insert(std::string name, type_1 * ptr) {
T1_var[name] = ptr;
}
apitemp operator[] (std::string n_) {
return apitemp(n_, this);
}
};
template<class out, class in1, class in2>
out hello_world(in1 name, in2 number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
template<class in1, class in2>
std::string hello_world(in1 name, in2 number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return std::string("Yep, we can!");
}
int main() {
int a = hello_world<int, std::string, const int &>("Tim", 25);
std::string b = hello_world<std::string, const int &>("Tim", 25);
my_map<int(std::string, const int &), std::string(std::string, const int &)> myMap;
myMap.insert("my_method_hello", &hello_world<int, std::string, const int &> );
myMap.insert("my_method_hello2", &hello_world<std::string, const int &> );
//int a = myMap["my_method_hello"]("Tim", 25); // error C3066: there are multiple ways that an object of this type can be called with these arguments
//std::string b = myMap["my_method_hello2"]("Tim", 25); // // error C3066: there are multiple ways that an object of this type can be called with these arguments
std::cin.get();
}
如何向其 API 引入多种返回类型函数? API用户方式是否可以隐形?或者至少有一些 API 用户令人不安的东西,比如
int a = myMap["my_method_hello"]("Tim", 25)::int;
std::string b = myMap["my_method_hello2"]("Tim", 25)::string;
?
最佳答案
一种方法是使用 myMap["my_method_hello"]("Tim", 25)
返回定义 operator int()
的代理对象, operator std::string()
, 等您希望它返回的每种类型。另一种方法是让代理对象显式定义 .asInt()
, .asString()
每种类型的方法。
如果目标类型在源代码中不明确(例如,如果您将结果传递给函数),则重载运算符技术可能会令人困惑。类似地,代理类型可能会混淆您传递给它的模板函数,并且如果候选集包含同时采用 int
的函数,将无法轻松选择重载函数。和 std::string
(或代理自动转换为的其他类型。因此,除了任何运算符之外,我建议提供 .asInt()
等功能。
您也可以拼写 .asInt()
等函数,如 template<typename T> as()
并使用显式特化来定义新的转换。唯一的缺点是源代码有点难以阅读。
关于c++ - 在函数映射容器 : automatic deduction fails with compiler error 中重载 C++ 函数返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8516690/