Results 1 to 12 of 12

Thread: C++ and C#

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default C++ and C#

    Hello,
    Do anyone know if how to implement something like istruction C# "yield" ??? (it permit to return a value (eg during a loop for) to another function and keep alive the environment).
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and C#

    Actually this instruction is not C# originated. I know it was present in at least Python before that. The easiest way is to use a static local variable holding the last returned result and continue from there during the next call.

    Qt Code:
    1. int getNextNatural(bool restart=false){
    2. static int last = -1;
    3. if(restart)
    4. last = -1;
    5. return ++last;
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: C++ and C#

    But I meant a bit more...
    Qt Code:
    1. vector<Element> Elements // 20 elements suppose
    2.  
    3. foo1() {
    4. foreach (Element e in checkForSomething(e)) {
    5. print (e.name);
    6. }
    7.  
    8. IEnumerable<Elemement> checkForSomething(e) {
    9. //iterator on Elements = eIt
    10. int a = 10; //it'll keep alive
    11. for(; eIt.MoveNext() ) { //movNext go on.....
    12. if (e.value ==2)
    13. yield return e;
    14. }
    15. }
    16. //main
    17. foo1();
    To copy to clipboard, switch view to plain text mode 
    when a Elements is ok, it's returned to foo1 and printed; after the control goes again inside checkForSOmething() (exactlly after the yield) and it restart from inside for at point it left......
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and C#

    Exactly what my code does... Make the iterator static and voila...

    Qt Code:
    1. ... {
    2. static XXX::const_iterator iter = e.begin();
    3. while(iter!=e.end()){
    4. //...
    5. return sth; // yields the iteration
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Next time you call the method, it will skip the initialization of the iterator and continue from where it left.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: C++ and C#

    yes, I could declare as static 'a' too....but when are these 'static' variabile deallocated? When program end, I guess...
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and C#

    I don't know what "a" does in your case because you don't use it anywhere, but yes, you may declare it static as well Static variables are never deallocated. They are destroyed together with global variables.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: C++ and C#

    Hello,
    'a' does nothing in my case. I'm looking for C# vs C++; with your code it's possibile to have something similar to yield; BUT: in my example what happen inside "for (moveNext)" is very simple; if I had many variabile, eg. "int a,b,c,d" or some large objects, I guess I have to declare they as statics -> they'll be keep alive until the application end! It seems a very waste of memory. Are we sure what is behind yield istruction does the same thing?? (ie work as "static" c++).

    Thank in advance.
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and C#

    Look - you are trying to emulate a construction of another language that doesn't exist in C++, so you have to accept some drawbacks of such solutions. I'm sure you can try to emulate yield using some mapping between objects and their current states to avoid static variables, but it's a bit more work. C# also has to keep those objects in memory because you might always want to call the function again and the state of the run has to be kept somewhere.

    But this is all unimportant - what is important is that C++ doesn't use a construction such as yield. In C++ you'd simply use an iterator passing it as an argument to the method (maybe even as a reference), like so:
    Qt Code:
    1. int func(std::vector<int>::const_iterator &iter){
    2. int val = *iter;
    3. ++iter;
    4. return val*2;
    5. }
    To copy to clipboard, switch view to plain text mode 

    In this function iter gets updated to point to the next entry, so next time you call the function, it will operate on the next index (provided that the iterator remains valid and unchanged).

  9. #9
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ and C#

    Actually it should be possible to emulate the whole IEnumerable interface with a class wrapping around a simple list structure.
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and C#

    Isn't it what iterators do?

  11. #11
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ and C#

    Quote Originally Posted by wysota View Post
    Isn't it what iterators do?
    Yes, it is almost exactly the same. The difference simply is that the yield keyword and the IEnumarable interface hide the whole iterator concept, and as such adds nothing but some syntactic sugar (to some, at least).
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ and C#

    Consider the following example:
    Qt Code:
    1. SomeContainerObject Elements;
    2. MyIterator iter(Elements);
    3. while(iter.hasNext()){
    4. Element e = iter.next();
    5. doSomething(e);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Isn't this exactly what is searched for? And this is exactly a typical iterator. Of course hasNext() and next() have to be implemented in such a way that they perform the same function what the yieldable function does.

    What is vital is that the iterator keeps an internal state of "index" in the iterated object. Then it's just a matter of generating elements. Of course the object doesn't even have to be a container - in that case the iterator becomes a simple generator function, like this:
    Qt Code:
    1. class Generator {
    2. public:
    3. Generator(int start){
    4. m_curr = start;
    5. }
    6. bool hasNext(){ return true; /* neverending story */ }
    7. int next(){ return -(m_curr++); }
    8. private:
    9. int m_curr;
    10. }
    To copy to clipboard, switch view to plain text mode 
    The above object keeps returning negations of increasing numbers starting from the number passed as the argument for the generator. It's similar to:
    Qt Code:
    1. int fun(int s){
    2. int x = s;
    3. while(1){
    4. int r = -x;
    5. x = x+1;
    6. yield r;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.