This is a detailed explanation about Gabriel's answer:
The Lambda function can be stored in a variable with type function<OutType<InType...>> as long as the header "functional" is included.
#include <iostream> #include <functional> // <- Required here. using namespace std; void put_until_bound(int start, int bound, int dx) { function<bool(int)> margin; // The value ``margin'' will be determined by the if branches... if (dx > 0) //Then we'll need an upper bound... margin = [bound] (int n) { return n >= bound; }; else if (dx < 0) // Then we'll need a lower bound... margin = [bound] (int n) { return n <= bound; }; else // By default bound is everywhere... margin = [] (int n) { return true; }; for (int x = start; ! margin(x); x += dx) cout<<x<<", "; cout<<endl; } // ... // Inside the main function: put_until_bound(10, 64, 8); // => 10, 18, 26, 34, 42, 50, 58, put_until_bound(10, -5, -2); // => 10, 8, 6, 4, 2, 0, -2, -4,
You should use the latest g++ with the compile command as follows:
g++ -O3 -o outfile -std=c++2a -fconcepts filename.cxx && ./outfile