extend.cpp 923 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/eval.hpp>
  6. #include <boost/hana/extend.hpp>
  7. #include <boost/hana/extract.hpp>
  8. #include <boost/hana/lazy.hpp>
  9. #include <functional>
  10. #include <istream>
  11. #include <sstream>
  12. namespace hana = boost::hana;
  13. template <typename T>
  14. T read_one(std::istream& s) {
  15. T value;
  16. s >> value;
  17. return value;
  18. }
  19. int main() {
  20. std::stringstream s;
  21. s << "1 2 3";
  22. auto from_stream = hana::extend(hana::make_lazy(read_one<int>)(std::ref(s)), [](auto i) {
  23. return hana::eval(i) + 1;
  24. });
  25. BOOST_HANA_RUNTIME_CHECK(hana::extract(from_stream) == 2);
  26. BOOST_HANA_RUNTIME_CHECK(hana::extract(from_stream) == 3);
  27. BOOST_HANA_RUNTIME_CHECK(hana::extract(from_stream) == 4);
  28. }