When using C++17, std::launder the reinterpreted pointer from std::aligned_storage to adapt to the change of object model introduced in P0137R1. Fix potential undefined behaviour.

C++17 introduced a change in the object model with P0137R1 which now requires the reinterpreted pointer from std::aligned_storage to be laundered. See the following discussion for some details https://stackoverflow.com/questions/47735657/does-reinterpret-casting-stdaligned-storage-to-t-without-stdlaunder-violat
This commit is contained in:
Tessil 2022-05-21 19:14:35 +01:00
parent c77f80b1b3
commit 4abcc978b9

View File

@ -33,6 +33,7 @@
#include <iterator>
#include <limits>
#include <memory>
#include <new>
#include <stdexcept>
#include <tuple>
#include <type_traits>
@ -249,12 +250,22 @@ class bucket_entry : public bucket_entry_hash<StoreHash> {
value_type& value() noexcept {
tsl_rh_assert(!empty());
#if defined(__cplusplus) && __cplusplus >= 201703L
return *std::launder(
reinterpret_cast<value_type*>(std::addressof(m_value)));
#else
return *reinterpret_cast<value_type*>(std::addressof(m_value));
#endif
}
const value_type& value() const noexcept {
tsl_rh_assert(!empty());
#if defined(__cplusplus) && __cplusplus >= 201703L
return *std::launder(
reinterpret_cast<const value_type*>(std::addressof(m_value)));
#else
return *reinterpret_cast<const value_type*>(std::addressof(m_value));
#endif
}
distance_type dist_from_ideal_bucket() const noexcept {