如何指示某些过载功能
How to indicate certain overloaded function?
例如,我们有一些C 代码,其中一些函数用某些参数执行另一个函数。
#include <iostream>
using namespace std;
template <typename T, typename F>
void todo(const T& param, const F& function)
{
function(param);
}
void foo(int a)
{
cout << a << endl;
}
int main()
{
int iA = 1051;
todo(iA, foo);
return 0;
}
但是,如果我们添加了一个名称foo
void foo(double a)
{
cout << a << endl;
}
然后编译器不知道哪个函数生成模板。
!!!重要!!!
这不是真实的代码,这就是示例。在通过重新加载函数作为参数中的问题。
有人知道如何明确指示某些功能吗?
todo(iA, static_cast<void (*)(int)>(&foo));
是一种方式,以呼叫网站的可读性为代价。
测试代码(保留您的2个字符凹痕 - 我是Myrmidon):
#include <iostream>
using namespace std;
template <typename T, typename F>
void todo(const T& param, const F& function)
{
function(param);
}
void foo(double a)
{
cout << "double " << a << endl;
}
void foo(int a)
{
cout << "int " << a << endl;
}
int main()
{
int iA = 1051;
todo(iA, static_cast<void (*)(int)>(&foo));
todo(iA, static_cast<void (*)(double)>(&foo));
return 0;
}
输出:
int 1051
double 1051
请参阅https://www.ideone.com/deilbe
auto identifier = type{value}
这是Sutter Mill在其AAA中建议的语法(几乎总是是自动),当时是第94周的GURU,当时需要进行特定类型。
它在这里完全适用:
int iA = 1051;
using foo_int_type = void(*)(int);
auto non_ambiguous_int_foo = foo_int_type{foo};
todo(iA, non_ambiguous_int_foo);
coliru上的现场演示
您可以使用static_cast
选择正确的功能超载。例如:todo(iA, static_cast<void(&)(double)>(foo))
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- Python中的for循环与C++有何不同
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 覆盖私有功能,它与受保护功能有何不同?
- 如何使一系列功能在C 中指示私有
- 功能和变量从何而来
- 如何指示某些过载功能
- 我不明白这些功能有何不同