c++ - 根据参数类型推断重载函数的函数类型

标签 c++ templates c++11 overloading overload-resolution

假设有一个重载函数:

void overloaded(int) {}
void overloaded(void) {}

因为我不想(或不能)写下完整的函数签名(如 void(int)void(void))我需要得到此签名仅使用函数名称(重载)和 它的参数类型(intvoid)。

我尝试了几种使用 decltype 和 friend 的方法,但不幸的是没有成功。

所以,简而言之,我需要这样的东西:

cout << typeid(
  get_overload(overloaded,(int)0))::type /* where the magic happens */
  .name() << endl;

最佳答案

如果您被允许使用名称 overloaded在 type 函数中,这将执行:

template<typename... A>
using sig = decltype(overloaded(std::declval<A>()...))(A...);

sig<int>给出 void(int) , 和 sig<>给出 void() .

这只是模板别名中 tclamb 解决方案的包装。

关于c++ - 根据参数类型推断重载函数的函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22491773/

相关文章:

c++ - 您如何确保调用 Derived 析构函数?

C++ 编写内联函数作为参数传递

c++ - 在二进制文件中搜索字符串的代码

c++ - Codeblocks graphics.h 不工作

c++ - 如何优雅地编写使用模板参数隐含值的类模板?

c++ - 我可以让我的 Makefile 自动让 GCC 使用它支持的最新标准吗?

html - perl 子程序输出附加 "1"

c++ - 如何确定将使用哪个模板

c++ - 模板元编程 NamedPipe 客户端服务器

c++ - 模板元编程如何专注于集合