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
+6 -4
View File
@@ -314,18 +314,20 @@ class Result {
}
}
T& get_t() noexcept { return *(reinterpret_cast<T*>(t_or_err_.data())); }
T& get_t() noexcept {
return *std::launder((reinterpret_cast<T*>(t_or_err_.data())));
}
const T& get_t() const noexcept {
return *(reinterpret_cast<const T*>(t_or_err_.data()));
return *std::launder((reinterpret_cast<const T*>(t_or_err_.data())));
}
Error& get_err() noexcept {
return *(reinterpret_cast<Error*>(t_or_err_.data()));
return *std::launder((reinterpret_cast<Error*>(t_or_err_.data())));
}
const Error& get_err() const noexcept {
return *(reinterpret_cast<const Error*>(t_or_err_.data()));
return *std::launder((reinterpret_cast<const Error*>(t_or_err_.data())));
}
void move_from_other(Result<T>& _other) noexcept {
+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>{}), ...);
}
+5 -6
View File
@@ -76,7 +76,7 @@ class Variant {
auto t = T{std::forward<Args>(_args)...};
destroy_if_necessary();
move_from_type(std::move(t));
return *(reinterpret_cast<T*>(data_.data()));
return *std::launder(reinterpret_cast<T*>(data_.data()));
}
/// Emplaces a new element into the variant.
@@ -225,7 +225,7 @@ class Variant {
index_ =
internal::element_index<CurrentType,
std::remove_cvref_t<AlternativeTypes>...>();
new (reinterpret_cast<CurrentType*>(data_.data())) CurrentType(_t);
new (data_.data()) CurrentType(_t);
}
void destroy_if_necessary() {
@@ -345,13 +345,13 @@ class Variant {
template <IndexType _i>
auto& get_alternative() noexcept {
using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>;
return *(reinterpret_cast<CurrentType*>(data_.data()));
return *std::launder(reinterpret_cast<CurrentType*>(data_.data()));
}
template <IndexType _i>
const auto& get_alternative() const noexcept {
using CurrentType = internal::nth_element_t<_i, AlternativeTypes...>;
return *(reinterpret_cast<const CurrentType*>(data_.data()));
return *std::launder(reinterpret_cast<const CurrentType*>(data_.data()));
}
void move_from_other(Variant<AlternativeTypes...>&& _other) noexcept {
@@ -367,8 +367,7 @@ class Variant {
index_ =
internal::element_index<CurrentType,
std::remove_cvref_t<AlternativeTypes>...>();
new (reinterpret_cast<CurrentType*>(data_.data()))
CurrentType(std::forward<T>(_t));
new (data_.data()) CurrentType(std::forward<T>(_t));
}
private:
+1 -1
View File
@@ -73,7 +73,7 @@ struct NamedTupleParser {
const R& _r, const InputVarType& _var) noexcept {
alignas(NamedTuple<FieldTypes...>) unsigned char
buf[sizeof(NamedTuple<FieldTypes...>)];
auto ptr = reinterpret_cast<NamedTuple<FieldTypes...>*>(buf);
auto ptr = std::launder(reinterpret_cast<NamedTuple<FieldTypes...>*>(buf));
auto view = rfl::to_view(*ptr);
using ViewType = std::remove_cvref_t<decltype(view)>;
const auto err =
+1 -1
View File
@@ -34,7 +34,7 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
[&](const InputArrayType& _arr) -> Result<std::array<T, _size>> {
alignas(
std::array<T, _size>) unsigned char buf[sizeof(std::array<T, _size>)];
auto ptr = reinterpret_cast<std::array<T, _size>*>(buf);
auto ptr = std::launder(reinterpret_cast<std::array<T, _size>*>(buf));
const auto array_reader =
ArrayReader<R, W, ProcessorsType, T, _size>(&_r, ptr);
auto err = _r.read_array(array_reader, _arr);
+5 -4
View File
@@ -9,9 +9,9 @@
#include "../always_false.hpp"
#include "../from_named_tuple.hpp"
#include "../internal/enums/StringConverter.hpp"
#include "../internal/has_reflector.hpp"
#include "../internal/has_reflection_method_v.hpp"
#include "../internal/has_reflection_type_v.hpp"
#include "../internal/has_reflector.hpp"
#include "../internal/is_basic_type.hpp"
#include "../internal/is_description.hpp"
#include "../internal/is_literal.hpp"
@@ -49,8 +49,9 @@ struct Parser {
return Error(e.what());
}
};
return Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::read(_r, _var).and_then(
wrap_in_t);
return Parser<R, W, typename Reflector<T>::ReflType,
ProcessorsType>::read(_r, _var)
.and_then(wrap_in_t);
} else if constexpr (R::template has_custom_constructor<T>) {
return _r.template use_custom_constructor<T>(_var);
} else {
@@ -237,7 +238,7 @@ struct Parser {
/// fields might not be default-constructible.
static Result<T> read_struct(const R& _r, const InputVarType& _var) {
alignas(T) unsigned char buf[sizeof(T)];
auto ptr = reinterpret_cast<T*>(buf);
auto ptr = std::launder(reinterpret_cast<T*>(buf));
auto view = ProcessorsType::template process<T>(to_view(*ptr));
using ViewType = std::remove_cvref_t<decltype(view)>;
const auto err =
+1 -1
View File
@@ -33,7 +33,7 @@ struct TupleParser {
const InputVarType& _var) noexcept {
const auto parse = [&](const InputArrayType& _arr) -> Result<TupleType> {
alignas(TupleType) unsigned char buf[sizeof(TupleType)];
auto ptr = reinterpret_cast<TupleType*>(buf);
auto ptr = std::launder(reinterpret_cast<TupleType*>(buf));
const auto tuple_reader =
TupleReader<R, W, TupleType, _ignore_empty_containers, _all_required,
ProcessorsType>(&_r, ptr);