Fix moves of any when value isInBuffer

This was copying the pointer to the other's value in other.buffer.
This commit is contained in:
Ben Clayton
2020-06-10 13:51:21 +01:00
parent 2f607e077a
commit f0c28f93dd
2 changed files with 27 additions and 2 deletions

View File

@@ -85,7 +85,13 @@ any::any(const any& other) noexcept : type(other.type) {
}
}
any::any(any&& other) noexcept : value(other.value), type(other.type) {
any::any(any&& other) noexcept : type(other.type) {
if (other.isInBuffer(other.value)) {
alloc(type->size(), type->alignment());
type->copyConstruct(value, other.value);
} else {
value = other.value;
}
other.value = nullptr;
other.type = nullptr;
}
@@ -110,8 +116,14 @@ any& any::operator=(const any& rhs) {
}
any& any::operator=(any&& rhs) noexcept {
value = rhs.value;
reset();
type = rhs.type;
if (rhs.isInBuffer(rhs.value)) {
alloc(type->size(), type->alignment());
type->copyConstruct(value, rhs.value);
} else {
value = rhs.value;
}
rhs.value = nullptr;
rhs.type = nullptr;
return *this;