Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generator (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C++=== It is possible to introduce generators into C++ using pre-processor macros. The resulting code might have aspects that are very different from native C++, but the generator syntax can be very uncluttered.<ref>{{Cite web|url=http://www.codeproject.com/KB/cpp/cpp_generators.aspx|title = Generators in C++|date = 21 September 2008}}</ref> The set of pre-processor macros defined in this source allow generators defined with the syntax as in the following example: <syntaxhighlight lang="cpp"> $generator(descent) { int i; // place the constructor of our generator, e.g. // descent(int minv, int maxv) {...} // from $emit to $stop is a body of our generator: $emit(int) // will emit int values. Start of body of the generator. for (i = 10; i > 0; --i) $yield(i); // similar to yield in Python, // returns next number in [1..10], reversed. $stop; // stop, end of sequence. End of body of the generator. }; </syntaxhighlight> This can then be iterated using: <syntaxhighlight lang="cpp"> int main(int argc, char* argv[]) { descent gen; for (int n; gen(n);) // "get next" generator invocation printf("next number is %d\n", n); return 0; } </syntaxhighlight> Moreover, [[C++11]] allows [[foreach loop]]s to be applied to any class that provides the <code>begin</code> and <code>end</code> functions. It's then possible to write generator-like classes by defining both the iterable methods (<code>begin</code> and <code>end</code>) and the iterator methods (<code>operator!=</code>, <code>operator++</code> and <code>operator*</code>) in the same class. For example, it is possible to write the following program: <syntaxhighlight lang="cpp"> #include <iostream> int main() { for (int i: range(10)) { std::cout << i << std::endl; } return 0; } </syntaxhighlight> A basic range implementation would look like that: <syntaxhighlight lang="cpp"> class range { private: int last; int iter; public: range(int end): last(end), iter(0) {} // Iterable functions const range& begin() const { return *this; } const range& end() const { return *this; } // Iterator functions bool operator!=(const range&) const { return iter < last; } void operator++() { ++iter; } int operator*() const { return iter; } }; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)