如何获得一个指向编译器选择的重载函数的函数指针? [英] How to get a function pointer to the overloaded function that the compiler chooses?

查看:164
本文介绍了如何获得一个指向编译器选择的重载函数的函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得一个函数指针,指向编译器在检查参数后选择的重载函数?在此示例中:

How can I get a function pointer to the overloaded function that the compiler would choose after inspecting the arguments? In this example:

#include <iostream>

void MyFunction(float a){}
void MyFunction(int a){}

int main()
{
  float a;

  MyFunction(a);

  void (*manualFunctionPointer)(float);
  manualFunctionPointer(a);

  // automaticFunctionPointer = ?
}



我已经指定我想要一个函数指针,并返回void。编译器肯定可以自己计算出来,因为MyFunction(a)调用调用正确的函数。有没有办法获得一个函数指针指向编译器选择的函数?

I have specified that I want a function pointer to the function that accepts a float and returns void. The compiler can certainly figure that out by itself, because the MyFunction(a) call calls the right function. Is there a way to get a function pointer to the function that the compiler chooses?

推荐答案



Replace the float in

void (*manualFunctionPointer)(float);

使用

void (*manualFunctionPointer)(decltype(a));

然后无论什么改变,manualFunctionPointer将会跟随

Then whatever a changes too, manualFunctionPointer will follow

这篇关于如何获得一个指向编译器选择的重载函数的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆