from_array.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*=============================================================================
  2. Copyright (c) 2017 Nikita Kniazev
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <boost/phoenix/core.hpp>
  7. #include <boost/phoenix/object.hpp>
  8. #include <boost/phoenix/scope.hpp>
  9. #include <boost/phoenix/operator.hpp>
  10. #include <string>
  11. #include <iostream>
  12. template <typename T, std::size_t N>
  13. struct array_holder
  14. {
  15. typedef T underlying_type[N];
  16. array_holder(underlying_type const& x)
  17. {
  18. for (std::size_t i = 0; i < N; ++i) elems_[i] = x[i];
  19. }
  20. T elems_[N];
  21. friend std::ostream& operator<<(std::ostream& os, array_holder const& x)
  22. {
  23. os << x.elems_[0];
  24. for (std::size_t i = 1; i < N; ++i) os << ", " << x.elems_[i];
  25. return os;
  26. }
  27. };
  28. int main()
  29. {
  30. using boost::phoenix::construct;
  31. using boost::phoenix::let;
  32. using boost::phoenix::arg_names::_1;
  33. using boost::phoenix::local_names::_a;
  34. let(_a = construct<std::string>("str"))
  35. [
  36. std::cout << _a << std::endl
  37. ]();
  38. let(_a = construct<array_holder<char, 4> >("str"))
  39. [
  40. std::cout << _a << std::endl
  41. ]();
  42. int ints[] = { 1, 2, 3 };
  43. let(_a = construct<array_holder<int, sizeof(ints) / sizeof(ints[0])> >(ints) )
  44. [
  45. std::cout << _a << std::endl
  46. ]();
  47. return 0;
  48. }