Skip to content

Commit

Permalink
Use std::variant::emplace when loading
Browse files Browse the repository at this point in the history
  • Loading branch information
kepler-5 authored and AzothAmmo committed Sep 17, 2021
1 parent 46a4a91 commit 0bbbb14
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions include/cereal/types/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,24 @@ namespace cereal
};

//! @internal
template<int N, class Variant, class ... Args, class Archive>
template<int N, class Variant, class Archive>
typename std::enable_if<N == std::variant_size_v<Variant>, void>::type
load_variant(Archive & /*ar*/, int /*target*/, Variant & /*variant*/)
{
throw ::cereal::Exception("Error traversing variant during load");
}
//! @internal
template<int N, class Variant, class H, class ... T, class Archive>
template<int N, class Variant, class Archive>
typename std::enable_if<N < std::variant_size_v<Variant>, void>::type
load_variant(Archive & ar, int target, Variant & variant)
{
if(N == target)
{
H value;
ar( CEREAL_NVP_("data", value) );
variant = std::move(value);
variant.template emplace<N>();
ar( CEREAL_NVP_("data", std::get<N>(variant)) );
}
else
load_variant<N+1, Variant, T...>(ar, target, variant);
load_variant<N+1>(ar, target, variant);
}

} // namespace variant_detail
Expand All @@ -98,7 +97,7 @@ namespace cereal
if(index >= static_cast<std::int32_t>(std::variant_size_v<variant_t>))
throw Exception("Invalid 'index' selector when deserializing std::variant");

variant_detail::load_variant<0, variant_t, VariantTypes...>(ar, index, variant);
variant_detail::load_variant<0>(ar, index, variant);
}

//! Serializing a std::monostate
Expand Down

0 comments on commit 0bbbb14

Please sign in to comment.