Date: Tue, 28 Oct 2025 16:46:34 +0500
No one really has correct code with std::varant right now, because no one
handles valueless_by_exception, but its possible to get rid of it.
0. backup memory of old value
1. construct new value ON TOP of memory of old value
On exception just memswap backup and new value (new value not constructed)
On success
2. memswap 2 buffers - with new value and backup
3. call destructor for old value
4. replace buffer without value with new value
Here pseudocode:
variant& operator= (variant&& v) {
// only if operator= may throw
// N - size of inner buffer
char buf[N];
// create "backup" for our value
memcpy(buf, m_buf, N);
on_scope_failure (return_value) {
swap_buffers(buf, m_buf);
};
// pseudocode, should be std::visit in real code
new (m_buf) T(std::move(v.current_value));
// success
return_value.no_longer_needed();
// swap for case when T was self reference, return it on its place for
dctor
swap_buffers(buf, m_buf);
// should be std::visit in real code + set real index
destroy_at((T*)m_buf;
// place new value on its place
memcpy(m_buf, buf, N);
return *this;
}
handles valueless_by_exception, but its possible to get rid of it.
0. backup memory of old value
1. construct new value ON TOP of memory of old value
On exception just memswap backup and new value (new value not constructed)
On success
2. memswap 2 buffers - with new value and backup
3. call destructor for old value
4. replace buffer without value with new value
Here pseudocode:
variant& operator= (variant&& v) {
// only if operator= may throw
// N - size of inner buffer
char buf[N];
// create "backup" for our value
memcpy(buf, m_buf, N);
on_scope_failure (return_value) {
swap_buffers(buf, m_buf);
};
// pseudocode, should be std::visit in real code
new (m_buf) T(std::move(v.current_value));
// success
return_value.no_longer_needed();
// swap for case when T was self reference, return it on its place for
dctor
swap_buffers(buf, m_buf);
// should be std::visit in real code + set real index
destroy_at((T*)m_buf;
// place new value on its place
memcpy(m_buf, buf, N);
return *this;
}
Received on 2025-10-28 11:46:45
