value_at.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*=============================================================================
  2. Copyright (c) 1999-2003 Jaakko Jarvi
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/fusion/sequence/intrinsic/at.hpp>
  9. #include <boost/fusion/sequence/intrinsic/value_at.hpp>
  10. #include <boost/static_assert.hpp>
  11. #include <iostream>
  12. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  13. #include <functional>
  14. #endif
  15. #if !defined(FUSION_AT)
  16. #define FUSION_AT at_c
  17. #endif
  18. #if !defined(FUSION_VALUE_AT)
  19. #define FUSION_VALUE_AT(S, N) boost::fusion::result_of::value_at_c<S, N>
  20. #endif
  21. namespace test_detail
  22. {
  23. // something to prevent warnings for unused variables
  24. template<class T> void dummy(const T&) {}
  25. class A {};
  26. }
  27. void
  28. test()
  29. {
  30. using namespace boost::fusion;
  31. using namespace test_detail;
  32. double d = 2.7;
  33. A a;
  34. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  35. // Note: C++11 will pickup the rvalue overload for the d argument
  36. // since we do not have all permutations (expensive!) for all const&
  37. // and && arguments. We either have all && or all const& arguments only.
  38. // For that matter, use std::ref to disambiguate the call.
  39. FUSION_SEQUENCE<int, double&, const A&, int> t(1, std::ref(d), a, 2);
  40. #else
  41. FUSION_SEQUENCE<int, double&, const A&, int> t(1, d, a, 2);
  42. #endif
  43. const FUSION_SEQUENCE<int, double&, const A, int> ct(t);
  44. int i = FUSION_AT<0>(t);
  45. int i2 = FUSION_AT<3>(t);
  46. BOOST_TEST(i == 1 && i2 == 2);
  47. int j = FUSION_AT<0>(ct);
  48. BOOST_TEST(j == 1);
  49. FUSION_AT<0>(t) = 5;
  50. BOOST_TEST(FUSION_AT<0>(t) == 5);
  51. #if defined(FUSION_TEST_FAIL)
  52. FUSION_AT<0>(ct) = 5; // can't assign to const
  53. #endif
  54. double e = FUSION_AT<1>(t);
  55. BOOST_TEST(e > 2.69 && e < 2.71);
  56. FUSION_AT<1>(t) = 3.14+i;
  57. BOOST_TEST(FUSION_AT<1>(t) > 4.13 && FUSION_AT<1>(t) < 4.15);
  58. #if defined(FUSION_TEST_FAIL)
  59. FUSION_AT<4>(t) = A(); // can't assign to const
  60. dummy(FUSION_AT<5>(ct)); // illegal index
  61. #endif
  62. ++FUSION_AT<0>(t);
  63. BOOST_TEST(FUSION_AT<0>(t) == 6);
  64. typedef FUSION_SEQUENCE<int, float> seq_type;
  65. BOOST_STATIC_ASSERT(!(
  66. boost::is_const<FUSION_VALUE_AT(seq_type, 0)::type>::value));
  67. // constness should not affect
  68. BOOST_STATIC_ASSERT(!(
  69. boost::is_const<FUSION_VALUE_AT(const seq_type, 0)::type>::value));
  70. BOOST_STATIC_ASSERT(!(
  71. boost::is_const<FUSION_VALUE_AT(seq_type, 1)::type>::value));
  72. // constness should not affect
  73. BOOST_STATIC_ASSERT(!(
  74. boost::is_const<FUSION_VALUE_AT(const seq_type, 1)::type>::value));
  75. dummy(i); dummy(i2); dummy(j); dummy(e); // avoid warns for unused variables
  76. }