Use std::launder where appropriate (#181)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2024-09-05 20:18:46 +02:00
committed by GitHub
parent b661b6147a
commit 7274a2eb47
7 changed files with 26 additions and 26 deletions
+7 -9
View File
@@ -57,14 +57,15 @@ class Tuple {
template <int _index>
auto& get() {
using Type = internal::nth_element_t<_index, Types...>;
return *(reinterpret_cast<Type*>(data_.data() + pos<_index>()));
return *std::launder(reinterpret_cast<Type*>(data_.data() + pos<_index>()));
}
/// Gets an element by index.
template <int _index>
const auto& get() const {
using Type = internal::nth_element_t<_index, Types...>;
return *(reinterpret_cast<const Type*>(data_.data() + pos<_index>()));
return *std::launder(
reinterpret_cast<const Type*>(data_.data() + pos<_index>()));
}
/// Assigns the underlying object.
@@ -131,8 +132,7 @@ class Tuple {
const auto copy_one = [this]<int _i>(const auto& _other,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
new (reinterpret_cast<Type*>(data_.data() + pos<_i>()))
Type(_other.template get<_i>());
new (data_.data() + pos<_i>()) Type(_other.template get<_i>());
};
(copy_one(_other, std::integral_constant<int, _is>{}), ...);
}
@@ -143,7 +143,7 @@ class Tuple {
const auto copy_one = [this]<int _i>(const auto& _t,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
new (reinterpret_cast<Type*>(data_.data() + pos<_i>())) Type(_t);
new (data_.data() + pos<_i>()) Type(_t);
};
(copy_one(_types, std::integral_constant<int, _is>{}), ...);
}
@@ -165,8 +165,7 @@ class Tuple {
const auto move_one = [this]<int _i>(auto&& _other,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
new (reinterpret_cast<Type*>(data_.data() + pos<_i>()))
Type(std::move(_other.template get<_i>()));
new (data_.data() + pos<_i>()) Type(std::move(_other.template get<_i>()));
};
(move_one(_other, std::integral_constant<int, _is>{}), ...);
}
@@ -176,8 +175,7 @@ class Tuple {
const auto move_one = [this]<int _i>(auto&& _t,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
new (reinterpret_cast<Type*>(data_.data() + pos<_i>()))
Type(std::move(_t));
new (data_.data() + pos<_i>()) Type(std::move(_t));
};
(move_one(std::move(_types), std::integral_constant<int, _is>{}), ...);
}