tuple-construct.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file tuple-construct.cc
  3. /// @brief If I were to construct Small from tuples
  4. // (c) Daniel Llorens - 2018
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <cassert>
  10. #include <iostream>
  11. #include "ra/tuples.hh"
  12. using std::tuple, std::cout, std::endl;
  13. template <class T, class sizes> struct nested_tuple;
  14. template <class T>
  15. struct nested_tuple<T, mp::int_list<>>
  16. {
  17. constexpr static int rank = 0;
  18. using type = T;
  19. };
  20. template <class T, class sizes>
  21. struct nested_tuple
  22. {
  23. constexpr static int rank = mp::len<sizes>;
  24. using sub = typename nested_tuple<T, mp::drop1<sizes>>::type;
  25. using type = mp::makelist<mp::ref<sizes, 0>::value, sub>;
  26. };
  27. struct foo
  28. {
  29. int x = true;
  30. foo(nested_tuple<int, mp::int_list<2, 3>>::type const & a) { cout << "A" << endl; }
  31. };
  32. int main()
  33. {
  34. using sizes0 = mp::int_list<>;
  35. using sizes1 = mp::int_list<3>;
  36. using sizes2 = mp::int_list<3, 4>;
  37. using sizes3 = mp::int_list<3, 4, 5>;
  38. {
  39. std::cout << nested_tuple<int, sizes0>::rank << std::endl;
  40. std::cout << nested_tuple<int, sizes1>::rank << std::endl;
  41. std::cout << nested_tuple<int, sizes2>::rank << std::endl;
  42. std::cout << nested_tuple<int, sizes3>::rank << std::endl;
  43. }
  44. // extra pair is required since it's not an initializer_list constructor.
  45. foo f( {{1, 2, 3}, {4, 5, 6}} );
  46. foo g{ {{1, 2, 3}, {4, 5, 6}} };
  47. foo h = { {{1, 2, 3}, {4, 5, 6}} };
  48. cout << f.x << endl;
  49. cout << g.x << endl;
  50. cout << h.x << endl;
  51. // extra pair isn't required for the tuples themselves though :-/
  52. nested_tuple<int, mp::int_list<2, 3>>::type i = {{1, 2, 3}, {4, 5, 6}};
  53. cout << foo(i).x << endl;
  54. return 0;
  55. }