Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent in C++ of Yield in C#?

Tags:

c++

public void Consumer() {     foreach(int i in Integers())     {         Console.WriteLine(i.ToString());     } }  public IEnumerable<int> Integers() {     yield return 1;     yield return 2;     yield return 4;     yield return 8;     yield return 16;     yield return 16777216; } 

Is there a way with template trick (or other) to get the same syntax in c++?

like image 239
Guillaume Paris Avatar asked Aug 27 '11 10:08

Guillaume Paris


People also ask

What is yield in C?

What is the use of yield return in C#? CsharpServer Side ProgrammingProgramming. Yield keyword helps to do custom stateful iteration over a collection. Meaning when we use yield keyword the control moves back and forth from the caller function to source and vice versa.

What does yield return do C#?

You use a yield return statement to return each element one at a time. The sequence returned from an iterator method can be consumed by using a foreach statement or LINQ query. Each iteration of the foreach loop calls the iterator method.

What is yield keyword?

The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword. yield can only be called directly from the generator function that contains it.

What is yield unity?

The yield return statement is special; it is what actually tells Unity to pause the script and continue on the next frame. There are a number of ways that can be used to yield return; one of which is to create an instance of the WaitForSeconds class.


2 Answers

Take a look at boost::Coroutine. It does what you want. http://www.crystalclearsoftware.com/soc/coroutine/index.html#coroutine.intro

Example from tutorial

http://www.crystalclearsoftware.com/soc/coroutine/coroutine/tutorial.html

int range_generator(generator_type::self& self, int min, int max)   {    while(min < max)      self.yield(min++);    self.exit();  } 
like image 109
Łukasz Milewski Avatar answered Sep 24 '22 02:09

Łukasz Milewski


You can always code this by hand. Truthfully, yield really seems like sugar coating to me (and co-routines too).

What a coroutine is, really ? Some state bundled up together with:

  • one function to create it (isn't it a constructor ?)
  • one function to move to the next state (isn't it operator++, traditionally ?)

In C++, it's called an InputIterator, and can be arbitrarily fat.

So, it's true that the syntax won't be as pretty, but this should do, just with the Standard Library:

static std::array<int, 6> const Array = {{1, 2, 4, 8, 16, 16777216}};  class Integers: public std::iterator<std::input_iterator_tag,                                       int, ptrdiff_t, int const*, int> { public:   Integers(): _index(0) {}    operator bool() const { return _index < Array.size(); }    Integers& operator++() { assert(*this); ++_index; return *this; }   Integers operator++(int) { Integers tmp = *this; ++*this; return tmp; }    int operator*() const { assert(*this); return Array[_index]; }   int const* operator->() const { assert(*this); return &Array[_index]; }  private:   size_t _index; }; // class Integers 

And obviously, since you decide exactly what state is stored, you decide if all is pre-computed or if part (or whole of it) is lazily computed, and possibly cached, and possibly multi-threaded, and ... you got the idea :)

like image 21
Matthieu M. Avatar answered Sep 21 '22 02:09

Matthieu M.