I struggle to understand what is happening with this snippet of code:
#include <iostream>
#include <variant>
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
struct FirstPSet {
std::uint16_t uid;
};
struct SecondPSet {
std::uint16_t uid;
};
using PSet = std::variant<std::monostate, FirstPSet, SecondPSet>;
auto getUID(const PSet& pSet) {
auto visitor = overloaded{[](const std::monostate&) -> std::uint16_t { return 0; },
[](const auto& pSet) -> std::uint16_t { return pSet.uid; }};
return std::visit(visitor, pSet);
}
int main() {
FirstPSet pSet{42};
std::cout << getUID(pSet) << '\n';
}
When compiled with Visual Studio Community 16.8.2 (debug build), I have a exception for a stack corruption at the end of getUID.
If i modify visitorto be auto&& visitor = ..., it works fine.