Skip to content

Commit

Permalink
Work around a VC++ bug in util::overload
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed May 24, 2024
1 parent 50892fa commit c468758
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
33 changes: 9 additions & 24 deletions src/realm/sync/changeset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,15 @@ InternString Changeset::find_string(StringData string) const noexcept

PrimaryKey Changeset::get_key(const Instruction::PrimaryKey& key) const noexcept
{
// we do not use the expected `mpark::visit(overload...` because in MSVC 2019 this
// code produces a segfault for something that works on other compilers.
// See https://github.com/realm/realm-core/issues/4624
if (const auto int64_ptr = mpark::get_if<int64_t>(&key)) {
return *int64_ptr;
}
else if (const auto intern_string_ptr = mpark::get_if<InternString>(&key)) {
return this->get_string(*intern_string_ptr);
}
else if (const auto monostate_ptr = mpark::get_if<mpark::monostate>(&key)) {
return *monostate_ptr;
}
else if (const auto global_key_ptr = mpark::get_if<GlobalKey>(&key)) {
return *global_key_ptr;
}
else if (const auto oid_ptr = mpark::get_if<ObjectId>(&key)) {
return *oid_ptr;
}
else if (const auto uuid_ptr = mpark::get_if<UUID>(&key)) {
return *uuid_ptr;
}
else {
REALM_UNREACHABLE(); // unhandled primary key type
}
return mpark::visit(overload{
[this](InternString str) -> PrimaryKey {
return get_string(str);
},
[](auto otherwise) -> PrimaryKey {
return otherwise;
},
},
key);
}

bool Changeset::operator==(const Changeset& that) const noexcept
Expand Down
6 changes: 6 additions & 0 deletions src/realm/util/overload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ namespace realm::util {
template <class... Ts>
struct overload : Ts... {
using Ts::operator()...;
#ifdef _MSC_VER
// https://developercommunity.visualstudio.com/t/runtime-stack-corruption-using-stdvisit/346200
// A bug in VC++'s Empty Base Optimization causes it to compute the wrong
// size if both the type and last lambda used have zero size
char dummy = 0;
#endif
};
template <class... Ts>
overload(Ts...) -> overload<Ts...>;
Expand Down

0 comments on commit c468758

Please sign in to comment.