#include "catch.hpp" #include #include // https://github.com/mapbox/variant/issues/21 static int count; struct t1 { int value; t1(t1 const& rhs) : value(rhs.value) { ++count; } t1(int v) : value(v) { ++count; } ~t1() { --count; } }; struct t2 { int value; t2(int v) : value(v) { // constructor fails throw std::runtime_error("fail"); } }; TEST_CASE("set() works cleanly even if the constructor throws ", "[variant]") { using variant_type = mapbox::util::variant; count = 0; { t1 obj{42}; variant_type v = obj; REQUIRE(v.is()); REQUIRE(v.get().value == 42); REQUIRE_THROWS({ v.set(13); }); } REQUIRE(count == 0); }