tuple_indexed.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Simple test file for cxxomfort's
  3. * "get<type> from std::tuple" feature extension.
  4. *
  5. * Interfaces tested in this example:
  6. *
  7. * * std::tuple
  8. * * std::get<type>(std::tuple<...>)
  9. * * cxxomfort::type_name
  10. * * static_assert
  11. *
  12. */
  13. #include <cxxomfort/cxxomfort.hpp>
  14. #include <cxxomfort/tuple.hpp>
  15. #include <cxxomfort/type_traits.hpp> // integral_constant
  16. #include <cxxomfort/utility.hpp>
  17. #include <cxxomfort/library/tuple.hpp>
  18. #include <cxxomfort/library/type_name.hpp>
  19. #include <iostream>
  20. typedef std::tuple<long int, bool, std::string> TupleType;
  21. template <typename TT>
  22. typename std::enable_if<(std::tuple_size<TT>::value > 1), std::ostream&>::type
  23. operator<< (std::ostream& os, TT const& tu) {
  24. using namespace std;
  25. os<< get<0>(tu)<< " ";
  26. os<< cxxomfort::library::tuple::tuple_shift(tu);
  27. return os;
  28. }
  29. template <typename TT>
  30. typename std::enable_if<(std::tuple_size<TT>::value == 1), std::ostream&>::type
  31. operator<< (std::ostream& os, TT const& tu) {
  32. using namespace std;
  33. return os<< get<0>(tu);
  34. }
  35. template <typename TT>
  36. struct TTindexed
  37. : public TT
  38. {
  39. static_assert (cxxomfort::library::tuple::is_tuple<TT>::value, "TT must be a tuple<...> type.");
  40. //TTindexed (TT& p)
  41. //: tt(&p) {}
  42. #if (CXXOMFORT_CXX_STD >= 2011)
  43. using TT::TT;
  44. #else
  45. TTindexed (TT& t)
  46. : TT(t) {}
  47. template <typename A0, typename A1>
  48. TTindexed (A0 a0, A1 a1)
  49. : TT(a0,a1) {}
  50. template <typename A0, typename A1, typename A2>
  51. TTindexed (A0 a0, A1 a1, A2 a2)
  52. : TT(a0,a1,a2) {}
  53. #endif
  54. //CXXO_CONSTEXPR explicit TTindexed (TT& tt) : TT(tt) {}
  55. template <unsigned I>
  56. typename std::tuple_element< I, TT >::type&
  57. operator[]
  58. (std::integral_constant<unsigned,I> const&) {
  59. using namespace std;
  60. return get<I>(*this) ;
  61. }
  62. template <unsigned I>
  63. typename std::tuple_element< I, TT >::type const&
  64. operator[]
  65. (std::integral_constant<unsigned,I> const&) const {
  66. return std::get<I>(*this) ;
  67. }
  68. private:
  69. //TT* const tt;
  70. };
  71. std::integral_constant<unsigned,0> I0c;
  72. std::integral_constant<unsigned,1> I1c;
  73. std::integral_constant<unsigned,2> I2c;
  74. std::integral_constant<unsigned,3> I3c;
  75. int main () {
  76. using namespace std;
  77. using namespace cxxomfort::library::tuple;
  78. cxxomfort::output_info(stdout);
  79. cout<< endl;
  80. cout<< cxxomfort::type_name<TupleType>()<< endl;
  81. TupleType tu1 (-7, false, string(4, 'h'));
  82. TTindexed<TupleType> ti1 (-7, true, string(7, 'p'));
  83. TTindexed<TupleType> tt1 ( tu1 );
  84. ti1[I0c]+= 107;
  85. ti1[I1c]= !ti1[I1c];
  86. cout<< (TupleType const&)(ti1)<< endl;
  87. }