construction.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/container/list/cons.hpp>
  10. #if !defined(FUSION_AT)
  11. #define FUSION_AT at_c
  12. #endif
  13. namespace test_detail
  14. {
  15. // something to prevent warnings for unused variables
  16. template<class T> void dummy(const T&) {}
  17. // no public default constructor
  18. class foo
  19. {
  20. public:
  21. explicit foo(int v) : val(v) {}
  22. bool operator==(const foo& other) const
  23. {
  24. return val == other.val;
  25. }
  26. private:
  27. foo() {}
  28. int val;
  29. };
  30. // another class without a public default constructor
  31. class no_def_constructor
  32. {
  33. no_def_constructor() {}
  34. public:
  35. no_def_constructor(std::string) {}
  36. };
  37. }
  38. inline void
  39. test()
  40. {
  41. using namespace boost::fusion;
  42. using namespace test_detail;
  43. nil empty;
  44. (void)empty;
  45. FUSION_SEQUENCE<> empty0;
  46. (void)empty0;
  47. #ifndef NO_CONSTRUCT_FROM_NIL
  48. FUSION_SEQUENCE<> empty1(empty);
  49. (void)empty1;
  50. #endif
  51. FUSION_SEQUENCE<int> t1;
  52. BOOST_TEST(FUSION_AT<0>(t1) == int());
  53. FUSION_SEQUENCE<float> t2(5.5f);
  54. BOOST_TEST(FUSION_AT<0>(t2) > 5.4f && FUSION_AT<0>(t2) < 5.6f);
  55. FUSION_SEQUENCE<foo> t3(foo(12));
  56. BOOST_TEST(FUSION_AT<0>(t3) == foo(12));
  57. FUSION_SEQUENCE<double> t4(t2);
  58. BOOST_TEST(FUSION_AT<0>(t4) > 5.4 && FUSION_AT<0>(t4) < 5.6);
  59. FUSION_SEQUENCE<int, float> t5;
  60. BOOST_TEST(FUSION_AT<0>(t5) == int());
  61. BOOST_TEST(FUSION_AT<1>(t5) == float());
  62. FUSION_SEQUENCE<int, float> t6(12, 5.5f);
  63. BOOST_TEST(FUSION_AT<0>(t6) == 12);
  64. BOOST_TEST(FUSION_AT<1>(t6) > 5.4f && FUSION_AT<1>(t6) < 5.6f);
  65. FUSION_SEQUENCE<int, float> t7(t6);
  66. BOOST_TEST(FUSION_AT<0>(t7) == 12);
  67. BOOST_TEST(FUSION_AT<1>(t7) > 5.4f && FUSION_AT<1>(t7) < 5.6f);
  68. FUSION_SEQUENCE<long, double> t8(t6);
  69. BOOST_TEST(FUSION_AT<0>(t8) == 12);
  70. BOOST_TEST(FUSION_AT<1>(t8) > 5.4f && FUSION_AT<1>(t8) < 5.6f);
  71. dummy
  72. (
  73. FUSION_SEQUENCE<no_def_constructor, no_def_constructor, no_def_constructor>(
  74. std::string("Jaba"), // ok, since the default
  75. std::string("Daba"), // constructor is not used
  76. std::string("Doo")
  77. )
  78. );
  79. dummy(FUSION_SEQUENCE<int, double>());
  80. dummy(FUSION_SEQUENCE<int, double>(1,3.14));
  81. #if defined(FUSION_TEST_FAIL)
  82. dummy(FUSION_SEQUENCE<double&>()); // should fail, no defaults for references
  83. dummy(FUSION_SEQUENCE<const double&>()); // likewise
  84. #endif
  85. {
  86. double dd = 5;
  87. dummy(FUSION_SEQUENCE<double&>(dd)); // ok
  88. dummy(FUSION_SEQUENCE<const double&>(dd+3.14)); // ok, but dangerous
  89. }
  90. #if defined(FUSION_TEST_FAIL)
  91. dummy(FUSION_SEQUENCE<double&>(dd+3.14)); // should fail,
  92. // temporary to non-const reference
  93. #endif
  94. }