gpt4 book ai didi

c++ - 确定仿函数的参数和结果类型

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:12:14 25 4
gpt4 key购买 nike

我如何测试仿函数是否是一个可调用对象,它引用一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com