mercredi 1 août 2018

STD::FUNCTION C++

I'm still learning Modern C++ and I would like to clarify STD:FUNCTION, Here is my sample code that works fine :

#include <iostream>
#include <functional>

using namespace std;

int func(function<bool()> foo) {
    return 2;
}

struct fee {
    bool operator()() {
        return true;
    }    
};

int main() {
    cout << func(fee());
}

It will return "2".

What I am wondering is why this does not work, I changed bool operator()() to bool operator()(int i).

#include <iostream>
#include <functional>

using namespace std;

int func(function<bool()> foo) {
    return 2;
}

struct fee {
    bool operator()(int i) {
        return true;
    }    
};

int main() {
    cout << func(fee());
}

The error says : In function 'int main()': 18:20: error: could not convert 'fee()' from 'fee' to 'std::function'

What should be the right thing to do ?

Aucun commentaire:

Enregistrer un commentaire