fibonacci.cpp 864 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright Oliver Kowalke 2016.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <memory>
  8. #include <boost/context/continuation.hpp>
  9. namespace ctx = boost::context;
  10. int main() {
  11. int a;
  12. ctx::continuation c=ctx::callcc(
  13. [&a](ctx::continuation && c){
  14. a=0;
  15. int b=1;
  16. for(;;){
  17. c=c.resume();
  18. int next=a+b;
  19. a=b;
  20. b=next;
  21. }
  22. return std::move( c);
  23. });
  24. for ( int j = 0; j < 10; ++j) {
  25. std::cout << a << " ";
  26. c=c.resume();
  27. }
  28. std::cout << std::endl;
  29. std::cout << "main: done" << std::endl;
  30. return EXIT_SUCCESS;
  31. }