task_parameter_lifetime.pass.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. #include <experimental/task>
  12. #include <cassert>
  13. #include "../counted.hpp"
  14. #include "../sync_wait.hpp"
  15. DEFINE_COUNTED_VARIABLES();
  16. void test_parameter_lifetime()
  17. {
  18. counted::reset();
  19. auto f = [](counted c) -> std::experimental::task<std::size_t>
  20. {
  21. co_return c.id();
  22. };
  23. {
  24. auto t = f({});
  25. assert(counted::active_instance_count() == 1);
  26. assert(counted::copy_constructor_count() == 0);
  27. assert(counted::move_constructor_count() <= 2); // Ideally <= 1
  28. auto id = sync_wait(t);
  29. assert(id == 1);
  30. assert(counted::active_instance_count() == 1);
  31. assert(counted::copy_constructor_count() == 0);
  32. // We are relying on C++17 copy-elision when passing the temporary counter
  33. // into f(). Then f() must move the parameter into the coroutine frame by
  34. // calling the move-constructor. This move could also potentially be
  35. // elided by the
  36. assert(counted::move_constructor_count() <= 1);
  37. }
  38. assert(counted::active_instance_count() == 0);
  39. }
  40. int main()
  41. {
  42. test_parameter_lifetime();
  43. return 0;
  44. }