c++ - 如何告诉函子模板类用户他必须编写什么函子 operator() 签名?
问题描述
考虑这个使用函子作为模板参数的模板类的无意义示例:(godbolt here)
volatile int a = 0;
volatile int b = 0;
template <typename F, typename F2>
class testclass
{
public:
bool doOperation(int a)
{
F f;
F2 f2;
f(a);
return f2(a);
}
};
int main()
{
struct MyFunctor1
{
bool operator()(int b)
{
a+=b;
return true;
}
};
struct MyFunctor2
{
bool operator()(int b)
{
a-=b;
return false;
}
};
testclass<MyFunctor1, MyFunctor2> instance;
instance.doOperation(b);
return 0;
}
让我们考虑testclass
用户不知道类的内部结构。所以他不知道他必须为模板参数提供的函子的 operator() 签名。
在这个示例中,我如何告诉用户传递一个其 operator() 签名如下的函子bool (int)
:以及如何强制编译器检查它?
解决方案
推荐阅读
- javascript - 异步/等待功能不等待
- bootstrap-4 - 容器超出 jumbotron
- flutter - 错误无法在此小部件上方找到正确的提供程序
- powershell - 有没有办法在对象创建时向用户显示 Powershell 类构造函数的可用重载,而不是让它们通过管道将其传递给 GM?
- python - 如何在 Pandas 中抑制科学记数法并在折线图末尾添加标签?
- javascript - 如何在 Asp.net 上实现 AgoraIO
- flutter - Flutter webview_flutter 构建失败
- android - 是否可以有单个 Android ConstraintLayout 可配置的水平或垂直方向?
- c# - 如何通过浏览器访问设备 USB - ASP .NET MVC
- python - python中类对象的类型是什么?