make_ready_future_pass.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011,2014 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/future.hpp>
  14. // class promise<R>
  15. // future<void> make_ready_future();
  16. // template <class T>
  17. // future<decay_t<T>> make_ready_future(T&&);
  18. // template <class T>
  19. // future<T> make_ready_future(remove_reference_t<T>&);
  20. // template <class T>
  21. // future<T> make_ready_future(remove_reference_t<T>&&);
  22. // template <class T, class ...Args>
  23. // future<T> make_ready_future(Args&& ... args);
  24. #define BOOST_THREAD_VERSION 3
  25. #include <boost/thread/future.hpp>
  26. #include <boost/detail/lightweight_test.hpp>
  27. #include <boost/static_assert.hpp>
  28. struct A
  29. {
  30. A() :
  31. value(0)
  32. {
  33. }
  34. A(int i) :
  35. value(i)
  36. {
  37. }
  38. A(int i, int j) :
  39. value(i+j)
  40. {
  41. }
  42. int value;
  43. };
  44. A make(int i) {
  45. return A(i);
  46. }
  47. A make(int i, int j) {
  48. return A(i, j);
  49. }
  50. struct movable2
  51. {
  52. int value_;
  53. BOOST_THREAD_MOVABLE_ONLY(movable2)
  54. movable2() : value_(1){}
  55. movable2(int i) : value_(i){}
  56. movable2(int i, int j) : value_(i+j){}
  57. //Move constructor and assignment
  58. movable2(BOOST_RV_REF(movable2) m)
  59. { value_ = m.value_; m.value_ = 0; }
  60. movable2 & operator=(BOOST_THREAD_RV_REF(movable2) m)
  61. { value_ = m.value_; m.value_ = 0; return *this; }
  62. bool moved() const //Observer
  63. { return !value_; }
  64. int value() const //Observer
  65. { return value_; }
  66. };
  67. movable2 move_return_function2(int i) {
  68. return movable2(i);
  69. }
  70. int main()
  71. {
  72. #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
  73. BOOST_STATIC_ASSERT((boost::is_copy_constructible<movable2>::value == false));
  74. BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<movable2>::value == true));
  75. BOOST_STATIC_ASSERT((boost::is_copy_constructible<A>::value == true));
  76. BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<A>::value == false));
  77. #endif
  78. {
  79. boost::future<void> f = boost::make_ready_future();
  80. f.wait();
  81. }
  82. {
  83. typedef A T;
  84. T i;
  85. boost::future<T> f = boost::make_ready_future(i);
  86. BOOST_TEST(f.get().value==0);
  87. }
  88. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  89. {
  90. typedef A T;
  91. boost::future<T> f = boost::make_ready_future<T>();
  92. BOOST_TEST(f.get().value==0);
  93. }
  94. {
  95. typedef A T;
  96. boost::future<T> f = boost::make_ready_future<T>(1);
  97. BOOST_TEST(f.get().value==1);
  98. }
  99. {
  100. typedef A T;
  101. boost::future<T> f = boost::make_ready_future<T>(1,2);
  102. BOOST_TEST(f.get().value==3);
  103. }
  104. {
  105. typedef A T;
  106. T i;
  107. boost::future<T&> f = boost::make_ready_future<T&>(i);
  108. BOOST_TEST(f.get().value==0);
  109. }
  110. #endif
  111. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  112. // sync/futures/make_ready_future_pass.cpp:125:65: erreur: conversion from Ôboost::future<boost::rv<movable2> >Õ to non-scalar type Ôboost::future<movable2>Õ requested
  113. {
  114. typedef movable2 T;
  115. T i;
  116. boost::future<T> f = boost::make_ready_future(boost::move(i));
  117. BOOST_TEST_EQ(f.get().value(),1);
  118. }
  119. #endif
  120. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  121. {
  122. typedef movable2 T;
  123. boost::future<T> f = boost::make_ready_future<T>();
  124. BOOST_TEST(f.get().value()==1);
  125. }
  126. {
  127. typedef movable2 T;
  128. boost::future<T> f = boost::make_ready_future<T>(1);
  129. BOOST_TEST(f.get().value()==1);
  130. }
  131. {
  132. typedef movable2 T;
  133. boost::future<T> f = boost::make_ready_future<T>(1,2);
  134. BOOST_TEST(f.get().value()==3);
  135. }
  136. #endif
  137. return boost::report_errors();
  138. }