tuple-construct.cc 2.1 KB

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