Skip to content

Commit

Permalink
Fix compilation error when std::variant has the same types
Browse files Browse the repository at this point in the history
  • Loading branch information
uyha committed Apr 2, 2024
1 parent e9e06a5 commit d812fa5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/msgpack/v1/adaptor/cpp17/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Variant construct_variant(
std::index_sequence<current_index, indices...>
) {
if constexpr(sizeof...(Ts) == 0) {
return object.as<T>();
return Variant{std::in_place_index<current_index>, object.as<T>()};
}
else {
if (index == current_index) {
return object.as<T>();
return Variant{std::in_place_index<current_index>, object.as<T>()};
}
return construct_variant<Variant, Ts...>(
index,
Expand Down
22 changes: 22 additions & 0 deletions test/msgpack_cpp17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,28 @@ BOOST_AUTO_TEST_CASE(variant_pack_unpack_as) {
oh.get().as<std::variant<bool, int, float, double> >();
BOOST_CHECK(val1 == val2);
BOOST_CHECK_THROW((oh.get().as<std::variant<bool>>()), msgpack::type_error);

{
std::stringstream same_ss;
std::variant<int, float, int> same_expected{std::in_place_index<2>, 2};
msgpack::pack(same_ss, same_expected);
std::string const& same_str = same_ss.str();
msgpack::object_handle same_oh =
msgpack::unpack(same_str.data(), same_str.size());
std::variant<int, float, int> same_actual = same_oh->as<std::variant<int, float, int>>();
BOOST_CHECK(same_expected == same_actual);
}

{
std::stringstream same_ss;
std::variant<int, int> same_expected{std::in_place_index<1>, 2};
msgpack::pack(same_ss, same_expected);
std::string const& same_str = same_ss.str();
msgpack::object_handle same_oh =
msgpack::unpack(same_str.data(), same_str.size());
std::variant<int, int> same_actual = same_oh->as<std::variant<int, int>>();
BOOST_CHECK(same_expected == same_actual);
}
}

BOOST_AUTO_TEST_CASE(variant_with_zone) {
Expand Down

0 comments on commit d812fa5

Please sign in to comment.