tie.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #if !defined(FUSION_AT)
  10. #define FUSION_AT at_c
  11. #endif
  12. #if !defined(FUSION_MAKE)
  13. #define FUSION_MAKE BOOST_PP_CAT(make_, FUSION_SEQUENCE)
  14. #endif
  15. #if !defined(FUSION_TIE)
  16. #define FUSION_TIE BOOST_PP_CAT(FUSION_SEQUENCE, _tie)
  17. #endif
  18. namespace test_detail
  19. {
  20. // something to prevent warnings for unused variables
  21. template<class T> void dummy(const T&) {}
  22. // no public default constructor
  23. class foo
  24. {
  25. public:
  26. explicit foo(int v) : val(v) {}
  27. bool operator==(const foo& other) const
  28. {
  29. return val == other.val;
  30. }
  31. private:
  32. foo() {}
  33. int val;
  34. };
  35. }
  36. void
  37. test()
  38. {
  39. using namespace boost::fusion;
  40. using namespace test_detail;
  41. int a;
  42. char b;
  43. foo c(5);
  44. FUSION_TIE(a, b, c) = FUSION_MAKE(2, 'a', foo(3));
  45. BOOST_TEST(a == 2);
  46. BOOST_TEST(b == 'a');
  47. BOOST_TEST(c == foo(3));
  48. FUSION_TIE(a, ignore, c) = FUSION_MAKE((short int)5, false, foo(5));
  49. BOOST_TEST(a == 5);
  50. BOOST_TEST(b == 'a');
  51. BOOST_TEST(c == foo(5));
  52. int i, j;
  53. FUSION_TIE(i, j) = FUSION_MAKE(1, 2);
  54. BOOST_TEST(i == 1 && j == 2);
  55. FUSION_SEQUENCE<int, int, float> ta;
  56. #if defined(FUSION_TEST_FAIL)
  57. ta = std::FUSION_MAKE(1, 2); // should fail, tuple is of length 3, not 2
  58. #endif
  59. dummy(ta);
  60. // ties cannot be rebound
  61. int d = 3;
  62. FUSION_SEQUENCE<int&> ti(a);
  63. BOOST_TEST(&FUSION_AT<0>(ti) == &a);
  64. ti = FUSION_SEQUENCE<int&>(d);
  65. BOOST_TEST(&FUSION_AT<0>(ti) == &a);
  66. }