mercredi 1 novembre 2017

Can I call a std::function from C?

I have some C++ code that returns a std::function. I would like to call this from some C code. Is this possible? As an example I have the following code:

typedef std::function<int(int)> AdderFunction;

AdderFunction makeAdder(int amount) {
    return [amount] (int n) {
        return n + amount;
    };
}

extern "C" {
    AdderFunction makeCAdder(int amount) {
        return makeAdder(amount);
    }
}

with clang++ -std=c++11 test.cpp it results in the following warning:

'makeCAdder' has C-linkage specified, but returns user-defined type 'AdderFunction' (aka 'function<int (int)>') which is incompatible with C

I understand why this is happening, but wondering if there is a pattern to make it possible?

Aucun commentaire:

Enregistrer un commentaire