make_exception_ptr.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: libcpp-no-exceptions
  9. // <exception>
  10. // template<class E> exception_ptr make_exception_ptr(E e);
  11. #include <exception>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. struct A
  15. {
  16. static int constructed;
  17. int data_;
  18. A(int data = 0) : data_(data) {++constructed;}
  19. ~A() {--constructed;}
  20. A(const A& a) : data_(a.data_) {++constructed;}
  21. };
  22. int A::constructed = 0;
  23. int main(int, char**)
  24. {
  25. {
  26. std::exception_ptr p = std::make_exception_ptr(A(5));
  27. try
  28. {
  29. std::rethrow_exception(p);
  30. assert(false);
  31. }
  32. catch (const A& a)
  33. {
  34. #ifndef _LIBCPP_ABI_MICROSOFT
  35. assert(A::constructed == 1);
  36. #else
  37. // On Windows exception_ptr copies the exception
  38. assert(A::constructed == 2);
  39. #endif
  40. assert(p != nullptr);
  41. p = nullptr;
  42. assert(p == nullptr);
  43. assert(a.data_ == 5);
  44. assert(A::constructed == 1);
  45. }
  46. assert(A::constructed == 0);
  47. }
  48. assert(A::constructed == 0);
  49. return 0;
  50. }