Does the following code invoke undefined behaviour?
std::variant<A,B> v = ...;
std::visit([&v](auto& e){
if constexpr (std::is_same_v<std::remove_reference_t<decltype(e)>,A>)
e.some_modifying_operation_on_A();
else {
int i = e.some_accessor_of_B();
v = some_function_returning_A(i);
}
}, v);
In particular, when the variant does not contain an A
,
this code re-assigns an A
while still holding a reference to the previously held object of type B
.
However, because the reference is not used anymore after the assignment,
I feel the code is fine.
However, would a standard-library be free to implement std::visit
in a way such that the above is undefined behaviour?