c++ - 如何转换重载的自由函数来解决重载冲突?

标签 c++

假设您有 2 个免费功能:

void do_something(dog d);
void do_something(cat c);

不要说您想将这些函数传递给模板函数:

template <typename DoSomethingFunc>
void do_something_template(DoSomethingFunc func);

以避免重载解析冲突的方式调用 do_something_template 的首选方法是什么?会不会是类型转换?

最佳答案

您可以强制转换或使用局部函数指针变量。

void (*p)(dog) = do_something;
do_something_template(p);

do_something_template(static_cast<void(*)(cat)>(do_something));

关于c++ - 如何转换重载的自由函数来解决重载冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17942299/

相关文章:

c++ - `char * const` 字段被 MSVC (VS2015) 视为 "incompatible with C"

c++ - 如何在 MSVC 中为传递给可变参数函数的非 POD 参数启用警告/错误?

C++/boost : Writing a more powerful sscanf replacement

c++ - 我的程序一直在循环,永远不会到达 "return 0;"。是编译器不好还是代码不好?

c++ - TBB:初始化 concurrent_hash_map

c# - SysInternal 的 ProcessMonitor 是如何工作的?

c++ - 基于 log4j 的记录器 : log4cpp vs log4cplus vs log4cxx

c++ - 调用 `clock()`会产生错误:对 `_times'的 undefined reference

c++ - <dirent.h> 在 Visual Studio 2010 或 2008 中

c++ - 如何读取动态大小的stringstream?