【发布时间】:2020-06-17 19:26:42
【问题描述】:
假设我有一个这样的函数f:
void f(int x) {
cout << "Hi I'm f" << endl;
}
auto x = f;
但是如果函数重载了,这个就不行了:
void f(int x) {}
void f(int x, const int y) {}
auto x = f; // Which one to use ?
所以这是不可能的。但是让我们假设我知道我将传递给函数的参数,例如我知道我想调用f(10, 20)。不是参数本身的类型,而是实际值,它们可能在 constness、reference-ness(假设这个词存在......)甚至通过强制转换的不同类型方面有所不同。在示例中,20 是 int&&,并且参数 const int 是可绑定的。我还假设没有边缘情况,并且值只能绑定到一个重载,即使直接调用也无法编译。是否可以推导出重载函数的地址和函数的签名?
伪代码:
Input: Values of arguments
Output: Address of overloaded function `f` OR function signature type (any one can be deduced from the other though)
明明可以直接调用,但是函数确实“偷偷摸摸”,直接调用,貌似不可能被引用。
template<typename... Args>
void callMeMaybe(Args... args) {
f(args...); // I can call f... But how to get f itself ?
auto x = static_cast<void(Args...)>(f) // does not works because f could differ in argument bind
}
【问题讨论】:
-
你要签名做什么?根据您正在做/试图完成的工作,您甚至可能不需要它。
-
根据您的开发环境,您可能会使用 __FUNCTION__ 或 __FUNC__ 预处理器,如下所述:gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
-
我想使用类似
std::bind的对象,该对象将按引用存储引用并按值存储值。std::bind按值存储他的所有参数,我不想使用std::ref或std::cref。还要注意的是,类型值/引用应该从f的签名中推断出来,而不是从std::bind-like 对象的调用(这里像callMeMaybe但它不起作用) -
您可以使用像
auto x = [](auto&&... x) -> decltype(f(std::forward<decltype(x)>(x)...)) { return f(std::forward<decltype(x)>(x)...); };这样的 lambda 来获得这种行为。如果需要,您只需要指定它如何捕获。 -
使用 lambdas,你试图解决的问题甚至不存在了。
标签: c++