vendredi 21 septembre 2018

C++ sequence generator with init value by function enclosure

I have a question: Is below piece of code valid and whether it is optimum for sequence generator function in C++14?

auto sequenceGen = [](int&& init) {
    int counter = init;
    return [counter=std::move(counter)]() mutable { return ++counter; };
};
auto getSeq1 = sequenceGen(100);
cout << getSeq1()<< endl;
cout << getSeq1()<< endl;

If not how should it be implemented?

Aucun commentaire:

Enregistrer un commentaire