tuple-construct.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include "ra/mpdebug.hh"
  13. using std::tuple, std::cout, std::endl;
  14. template <class T, class sizes> struct nested_tuple;
  15. template <class T>
  16. struct nested_tuple<T, mp::int_list<>>
  17. {
  18. constexpr static int rank = 0;
  19. using type = T;
  20. };
  21. template <class T, class sizes>
  22. struct nested_tuple
  23. {
  24. constexpr static int rank = mp::len<sizes>;
  25. using sub = typename nested_tuple<T, mp::drop1<sizes>>::type;
  26. using type = mp::makelist<mp::ref<sizes, 0>::value, sub>;
  27. };
  28. struct foo
  29. {
  30. int x = true;
  31. foo(nested_tuple<int, mp::int_list<2, 3>>::type const & a) { cout << "A" << endl; }
  32. };
  33. int main()
  34. {
  35. using sizes0 = mp::int_list<>;
  36. using sizes1 = mp::int_list<3>;
  37. using sizes2 = mp::int_list<3, 4>;
  38. using sizes3 = 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, mp::int_list<2, 3>>::type i = {{1, 2, 3}, {4, 5, 6}};
  54. cout << foo(i).x << endl;
  55. return 0;
  56. }