tuple-construct.C 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file tuple-construct.C
  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/tuple-dynamic.H"
  12. #include "ra/tuple-list.H"
  13. #include "ra/mpdebug.H"
  14. using std::tuple, std::cout, std::endl;
  15. template <class T, class sizes> struct nested_tuple;
  16. template <class T>
  17. struct nested_tuple<T, mp::int_list<>>
  18. {
  19. constexpr static int rank = 0;
  20. using type = T;
  21. };
  22. template <class T, class sizes>
  23. struct nested_tuple
  24. {
  25. constexpr static int rank = mp::len<sizes>;
  26. using sub = typename nested_tuple<T, mp::drop1<sizes>>::type;
  27. using type = mp::makelist<mp::ref<sizes, 0>::value, sub>;
  28. };
  29. struct foo
  30. {
  31. int x = true;
  32. foo(nested_tuple<int, mp::int_list<2, 3>>::type const & a) { cout << "A" << endl; }
  33. };
  34. int main()
  35. {
  36. using sizes0 = mp::int_list<>;
  37. using sizes1 = mp::int_list<3>;
  38. using sizes2 = mp::int_list<3, 4>;
  39. using sizes3 = mp::int_list<3, 4, 5>;
  40. {
  41. std::cout << nested_tuple<int, sizes0>::rank << std::endl;
  42. std::cout << nested_tuple<int, sizes1>::rank << std::endl;
  43. std::cout << nested_tuple<int, sizes2>::rank << std::endl;
  44. std::cout << nested_tuple<int, sizes3>::rank << std::endl;
  45. }
  46. // extra pair is required since it's not an initializer_list constructor.
  47. foo f( {{1, 2, 3}, {4, 5, 6}} );
  48. foo g{ {{1, 2, 3}, {4, 5, 6}} };
  49. foo h = { {{1, 2, 3}, {4, 5, 6}} };
  50. cout << f.x << endl;
  51. cout << g.x << endl;
  52. cout << h.x << endl;
  53. // extra pair isn't required for the tuples themselves though :-/
  54. nested_tuple<int, mp::int_list<2, 3>>::type i = {{1, 2, 3}, {4, 5, 6}};
  55. cout << foo(i).x << endl;
  56. return 0;
  57. }