mercredi 29 janvier 2020

tail recursion optimization

how to make tail recursion optimization enabled, when function more and vals call each other?

now, when I set n = 5, but when I set n = 50000000, it will occur error.

using Cb = std::function<void(bool, int)>;
using Read = std::function<void(bool, Cb)>;
using Sink = std::function<void(Read&)>;
using Through = std::function<Read(Read&)>;

int main() {
    int n = 5;

    Read vals = [&](bool abort, Cb cb) {
        if (n-- <= 0) {
            return;
        }
        cb(false, n); // call `more` function
    };

    Sink logInt = [&](Read &read) {
        Cb more;
        more = [&](bool abort, int val) {
            cout << val << endl;
            read(false, more); // call `vals` function
        };

        read(false, more);
    };

    logInt(vals);


    return 0;
}

whole real code example

Aucun commentaire:

Enregistrer un commentaire