vendredi 1 décembre 2017

templated function as a parameter to another templated function

I have an utility function which takes two values and does something on another object if two values meet a certain criteria.

So, the utility function has to take a member function as a std:function and also sometimes as a free flowing function.

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));

The above code is my dummy implementation, but doesn't work. Can someone suggest me a better code ?

Aucun commentaire:

Enregistrer un commentaire