Closed devyte closed 3 weeks ago
After poking at this for several days I found a workaround: I wrapped the call to boost::pfr::get<> in a template function and then specialized it for the non-const case:
// Get a class's member reference by index
template <size_t Idx, typename T>
constexpr auto& Member(const T& classObj)
{
return boost::pfr::get<Idx>(classObj);
}
// Get a class's member reference by index
// Workaround for issue in PFR issue https://github.com/boostorg/pfr/issues/190
// Assumes that the field isn't const in a non-const aggregate.
template <size_t Idx, typename T>
constexpr auto& Member(T& classObj)
{
auto const& classObjConst = classObj;
auto& fieldConst = boost::pfr::get<Idx>(classObjConst);
auto& field = const_cast<std::decay_t<decltype(fieldConst)>&>(fieldConst);
return field;
}
Although very dirty, this worked as expected. However, I then encountered a different crash elsewhere in my code in the move constructor of std::vector, which made zero sense, so I started to doubt the compiler, my existence, and the universe as a whole. In summary, I had installed the boost v1.86 libs via nuget package, so I decided to go back to basics and try my same code built in Linux with clang++ and linked against a clean build of the boost libs v1.86 built from source, with clang++, using C++20. No more issues.
So I'm guessing what I observed was either an issue with how the boost libs got built in the nuget package (https://github.com/sergey-shandar/getboost), or an issue with MSVC, or a combination of both.
Closing.
This is a weird one, I'm not sure what to make of it. Platform:
MCVE:
I tried to follow with the debugger into the pfr headers, and I reached core17.hpp where I found this:
For the case of non-const MyStruct, the fields_count_tag seems to have a wrong value of 4. I tried to go deeper into fields_count, but I don't understand how its constexpr std::size_t result is calculated, I got stuck at the following, which I don't understand: