#include "catch.hpp" #include "variant.hpp" #include "variant_io.hpp" // https://github.com/mapbox/variant/issues/21 static int count; struct t1 { int value; 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; { variant_type v{42}; REQUIRE(v.is()); REQUIRE(v.get().value == 42); REQUIRE_THROWS({ v.set(13); }); } REQUIRE(count == 0); }