我如何测试仿函数是否是一个可调用对象,它引用一个 int 并返回一个 bool?
template<typename functor>
void foo(functor f)
{
static_assert('functor == bool (int&)', "error message");
int x = -1;
if (f(x))
std::cout << x << std::endl;
}
bool bar(int& x)
{
x = 4711;
return true;
}
struct other
{
bool operator()(int& x)
{
x = 815;
return true;
}
};
最佳答案
在我看来,您并不是真的想检查仿函数的签名,而是首先要限制用户可以传入的内容:
如果您有权访问 std::function
,您可以这样做:
void foo(const std::function<bool(int&)>& f)
关于c++ - 确定仿函数的参数和结果类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014505/