test.cpp 916 B

1234567891011121314151617181920212223242526272829303132
  1. struct no_throw {
  2. no_throw(std::string i) : i(i) {}
  3. std::string i;
  4. };
  5. struct canthrow_move {
  6. canthrow_move(std::string i) : i(i) {}
  7. canthrow_move(canthrow_move const &) = default;
  8. canthrow_move(canthrow_move &&other) noexcept(false) : i(other.i) {}
  9. canthrow_move &operator=(canthrow_move &&) = default;
  10. std::string i;
  11. };
  12. bool should_throw = false;
  13. struct willthrow_move {
  14. willthrow_move(std::string i) : i(i) {}
  15. willthrow_move(willthrow_move const &) = default;
  16. willthrow_move(willthrow_move &&other) : i(other.i) {
  17. if (should_throw)
  18. throw 0;
  19. }
  20. willthrow_move &operator=(willthrow_move &&) = default;
  21. std::string i;
  22. };
  23. int main() {
  24. std::string s1 = "abcdefghijklmnopqrstuvwxyz";
  25. std::string s2 = "zyxwvutsrqponmlkjihgfedcbaxxx";
  26. tl::expected<no_throw, willthrow_move> a{s1};
  27. tl::expected<no_throw, willthrow_move> b{tl::unexpect, s2};
  28. should_throw = 1;
  29. swap(a, b);
  30. }