使用可调用参数重载可调用项
Overloading on callable with callable parameter
我正在尝试将下面这样的C#代码翻译成C++:
void SomeCall(Action action)
{
// do things like action();
}
void SomeCall(Action<Action> action)
{
// define some action1
// do things like action(action1);
}
SomeCall的C++等价物应该能够接受std::函数以及相同签名的内联和大纲C++lambda。
在浏览了许多关于C++std::function和lambdas上重载的SO问题后,似乎答案应该是:
template<typename Func>
enable_if<Func is something callable>
void SomeCall(Func&& action)
{
...
}
template<typename Func>
enable_if<Func is something callable taking another callable as the parameter>
void SomeCall(Func&& action)
{
...
}
你能帮我填空吗?
您可以尝试使用标准重载,例如:
void Fn( std::function<int(int)>& fn )
{
}
void Fn( std::function<int(float)>& fn )
{
}
如果这是不可接受的,你将不得不对模板元编程进行大量研究,以使enable_If按照你想要的方式工作,这是可以想象到的最虐待狂的编程形式。不过,严肃地说,你可以试着从安德烈·亚历山德雷斯库的《现代C++设计》开始。
相关文章:
- 调用重载的"<大括号括起来的初始值设定项列表>"对于对来说就足够了是模棱两可的
- 为什么初始化时没有调用重载赋值运算符?
- 使用 MINGW gcc 编译时,不会为 std::string 调用重载的新运算符
- 为什么使用不匹配的参数调用重载函数仍然有效
- 无法弄清楚为什么没有调用重载运算符 []
- 使用 nullptr 调用重载方法是不明确的
- C++编译时检查是否可以用某种类型的参数调用重载函数
- 在 if 语句中调用重载构造函数失败
- 为什么在析构函数中引发异常时不调用重载删除
- 从派生类调用重载函数
- 传递 const int* 和 int* 时调用重载函数的不同版本(const int* const&/&&)
- C++ - 在 std::thread 中调用重载函数时编译失败
- 为在与类方法中的类相同的命名空间中定义的结构调用重载运算符
- 从 C++ 中的子类调用重载父类的方法
- 在C++中,如何从父类变量的子类调用重载方法
- 使用指针调用重载而不分配新内存
- 如何在复制构造函数中调用重载的下标
- DetectMultiScale在使用rejectLevels和levelWeights调用重载时从不返回
- 从对象指针调用重载运算符 ()
- C++ 从类中调用重载运算符