variant_copy_assign_throw.cpp 999 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2019 Peter Dimov
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #if defined(_MSC_VER)
  8. # pragma warning( disable: 4702 ) // unreachable code
  9. #endif
  10. #include <boost/variant2/variant.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <stdexcept>
  13. using namespace boost::variant2;
  14. struct Y1
  15. {
  16. Y1() noexcept {} // =default fails on msvc-14.0
  17. Y1(Y1 const&)
  18. {
  19. throw std::runtime_error( "Y1(Y1 const&)" );
  20. }
  21. Y1& operator=(Y1 const&) = default;
  22. };
  23. struct Y2
  24. {
  25. Y2() noexcept {}
  26. Y2(Y2 const&)
  27. {
  28. throw std::runtime_error( "Y2(Y2 const&)" );
  29. }
  30. Y2& operator=(Y2 const&) = default;
  31. };
  32. void test()
  33. {
  34. variant<Y1, Y2> v1( in_place_type_t<Y1>{} );
  35. variant<Y1, Y2> v2( in_place_type_t<Y2>{} );
  36. BOOST_TEST_THROWS( v1 = v2, std::runtime_error )
  37. }
  38. int main()
  39. {
  40. test();
  41. return boost::report_errors();
  42. }