作为另一个模板函数的参数的模板函数

templated function as a parameter to another templated function

本文关键字:函数 参数 另一个      更新时间:2023-10-16

i具有一个实用程序函数,该函数需要两个值,如果两个值满足某个标准,则在另一个对象上做某事。

因此,实用程序函数必须将成员函数作为std:函数,有时还作为自由流动函数。

class A
{
    public:
    void fun(int a) {}
};
template <typename T>
bool ifSet(T a, T b, std::function<void(T)> f )
{
    if (a == b) return false;
    else return f(b);
}
int main() {
    auto p = std::make_shared<A>(new A);
    std::cout<< ifSet(10, 10, std::bind(A::fun, p, std::placeholders::_1));

上面的代码是我的虚拟实现,但行不通。有人可以建议我一个更好的代码吗?

您的

std::function<void(T)> f

返回 void ,然后将其用作返回 bool ifset()function