test_8600.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2013 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/assert.hpp>
  6. #include <boost/static_assert.hpp>
  7. #include <vector>
  8. #include <utility>
  9. #include <type_traits>
  10. #if 1
  11. struct B {
  12. int v;
  13. B(int i) : v(i) {}
  14. };
  15. struct D: B {
  16. D(int i) : B(i) {}
  17. };
  18. void fb(B const&) {}
  19. void fd(D const&) {}
  20. BOOST_STATIC_ASSERT(sizeof(B)==sizeof(D));
  21. template <class T, class Allocator=std::allocator<T> >
  22. class new_vector;
  23. template <class T, class Allocator>
  24. class new_vector : public std::vector<T,Allocator>
  25. {
  26. typedef std::vector<T,Allocator> base_type;
  27. public:
  28. new_vector() : base_type() {}
  29. new_vector(unsigned s) : base_type(s) {}
  30. };
  31. template <class Allocator >
  32. class new_vector<bool, Allocator>
  33. {
  34. //std::vector<char,typename Allocator::template rebind<char>::other > v;
  35. int i;
  36. public:
  37. };
  38. template <class T, class A>
  39. typename std::enable_if<!std::is_same<T, bool>::value,
  40. new_vector<T,A>&
  41. >::type
  42. new_vector_cast(std::vector<T,A> & v) {
  43. return reinterpret_cast<new_vector<T,A>&>(v);
  44. }
  45. BOOST_STATIC_ASSERT(sizeof(std::vector<int>)==sizeof(new_vector<int>));
  46. BOOST_STATIC_ASSERT(sizeof(std::vector<bool>)!=sizeof(new_vector<bool>));
  47. void fb(std::vector<int> const&) {}
  48. void fd(new_vector<int> const&) {}
  49. int main() {
  50. {
  51. std::vector<int> b(1);
  52. b[0] = 1;
  53. new_vector<int> d = new_vector_cast(b);
  54. BOOST_ASSERT(b[0] == d[0]);
  55. }
  56. {
  57. //std::vector<bool> b;
  58. //new_vector<bool> d = new_vector_cast(b); // compile fail
  59. }
  60. {
  61. std::vector<int> b(1);
  62. b[0] = 1;
  63. fd(new_vector_cast(b));
  64. }
  65. {
  66. new_vector<int> d(1);
  67. d[0] = 1;
  68. std::vector<int> b = d;
  69. BOOST_ASSERT(b[0] == d[0]);
  70. }
  71. {
  72. //new_vector<bool> d;
  73. //std::vector<bool> b = d; // compile fail
  74. }
  75. {
  76. new_vector<int> d(1);
  77. d[0] = 1;
  78. fd(d);
  79. }
  80. return 0;
  81. }
  82. #else
  83. int main() {
  84. {
  85. B b(1);
  86. D d = reinterpret_cast<D&>(b);
  87. BOOST_ASSERT(b.v == d.v);
  88. }
  89. {
  90. B b(1);
  91. fd(reinterpret_cast<D&>(b));
  92. }
  93. {
  94. D d(2);
  95. B b = d;
  96. BOOST_ASSERT(b.v == d.v);
  97. }
  98. {
  99. D d(2);
  100. fd(d);
  101. }
  102. return 0;
  103. }
  104. #define BOOST_THREAD_VERSION 4
  105. #include <iostream>
  106. #include <boost/thread.hpp>
  107. int calculate_the_answer_to_life_the_universe_and_everything()
  108. {
  109. return 42;
  110. }
  111. int main()
  112. {
  113. boost::packaged_task<int()> pt(calculate_the_answer_to_life_the_universe_and_everything);
  114. boost::shared_future<int> fi1 = boost::shared_future<int>(pt.get_future());
  115. boost::shared_future<int> fi2 = fi1;
  116. boost::thread task(boost::move(pt)); // launch task on a thread
  117. boost::wait_for_any(fi1, fi2);
  118. std::cout << "Wait for any returned\n";
  119. return (0);
  120. }
  121. #endif