map_construction.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*=============================================================================
  2. Copyright (c) 1999-2003 Jaakko Jarvi
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Copyright (c) 2006 Dan Marsden
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/fusion/container/map/map.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/fusion/sequence/intrinsic/at.hpp>
  11. struct key1 {};
  12. struct key2 {};
  13. struct key3 {};
  14. namespace test_detail
  15. {
  16. // something to prevent warnings for unused variables
  17. template<class T> void dummy(const T&) {}
  18. // no public default constructor
  19. class foo
  20. {
  21. public:
  22. explicit foo(int v) : val(v) {}
  23. bool operator==(const foo& other) const
  24. {
  25. return val == other.val;
  26. }
  27. private:
  28. foo() {}
  29. int val;
  30. };
  31. // another class without a public default constructor
  32. class no_def_constructor
  33. {
  34. no_def_constructor() {}
  35. public:
  36. no_def_constructor(std::string) {}
  37. };
  38. }
  39. inline void
  40. test()
  41. {
  42. using namespace boost::fusion;
  43. using namespace test_detail;
  44. nil empty;
  45. (void)empty;
  46. map<> empty0;
  47. (void)empty0;
  48. #ifndef NO_CONSTRUCT_FROM_NIL
  49. map<> empty1(empty);
  50. (void)empty1;
  51. #endif
  52. map<pair<key1, int> > t1;
  53. BOOST_TEST(at_c<0>(t1).second == int());
  54. map<pair<key1, float> > t2(5.5f);
  55. BOOST_TEST(at_c<0>(t2).second > 5.4f && at_c<0>(t2).second < 5.6f);
  56. map<pair<key1, foo> > t3(foo(12));
  57. BOOST_TEST(at_c<0>(t3).second == foo(12));
  58. map<pair<key1, double> > t4(t2);
  59. BOOST_TEST(at_c<0>(t4).second > 5.4 && at_c<0>(t4).second < 5.6);
  60. map<pair<key1, int>, pair<key2, float> > t5;
  61. BOOST_TEST(at_c<0>(t5).second == int());
  62. BOOST_TEST(at_c<1>(t5).second == float());
  63. map<pair<key1, int>, pair<key2, float> > t6(12, 5.5f);
  64. BOOST_TEST(at_c<0>(t6).second == 12);
  65. BOOST_TEST(at_c<1>(t6).second > 5.4f && at_c<1>(t6).second < 5.6f);
  66. map<pair<key1, int>, pair<key2, float> > t7(t6);
  67. BOOST_TEST(at_c<0>(t7).second == 12);
  68. BOOST_TEST(at_c<1>(t7).second > 5.4f && at_c<1>(t7).second < 5.6f);
  69. map<pair<key1, long>, pair<key2, double> > t8(t6);
  70. BOOST_TEST(at_c<0>(t8).second == 12);
  71. BOOST_TEST(at_c<1>(t8).second > 5.4f && at_c<1>(t8).second < 5.6f);
  72. dummy
  73. (
  74. map<
  75. pair<key1, no_def_constructor>,
  76. pair<key2, no_def_constructor>,
  77. pair<key3, no_def_constructor> >
  78. (
  79. pair<key1, no_def_constructor>(std::string("Jaba")), // ok, since the default
  80. pair<key2, no_def_constructor>(std::string("Daba")), // constructor is not used
  81. pair<key3, no_def_constructor>(std::string("Doo"))
  82. )
  83. );
  84. dummy(map<pair<key1, int>, pair<key2, double> >());
  85. dummy(map<pair<key1, int>, pair<key2, double> >(1,3.14));
  86. #if defined(FUSION_TEST_FAIL)
  87. dummy(map<pair<key1, double&> >()); // should fail, no defaults for references
  88. dummy(map<pair<key1, const double&> >()); // likewise
  89. #endif
  90. {
  91. double dd = 5;
  92. dummy(map<pair<key1, double&> >(pair<key1, double&>(dd))); // ok
  93. dummy(map<pair<key1, const double&> >(pair<key1, const double&>(dd+3.14))); // ok, but dangerous
  94. }
  95. #if defined(FUSION_TEST_FAIL)
  96. dummy(map<pair<key1, double&> >(dd+3.14)); // should fail,
  97. // temporary to non-const reference
  98. #endif
  99. }
  100. int
  101. main()
  102. {
  103. test();
  104. return boost::report_errors();
  105. }