Skip to content

Commit 4abcc97

Browse files
committed
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
1 parent c77f80b commit 4abcc97

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

include/tsl/robin_hash.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <iterator>
3434
#include <limits>
3535
#include <memory>
36+
#include <new>
3637
#include <stdexcept>
3738
#include <tuple>
3839
#include <type_traits>
@@ -249,12 +250,22 @@ class bucket_entry : public bucket_entry_hash<StoreHash> {
249250

250251
value_type& value() noexcept {
251252
tsl_rh_assert(!empty());
253+
#if defined(__cplusplus) && __cplusplus >= 201703L
254+
return *std::launder(
255+
reinterpret_cast<value_type*>(std::addressof(m_value)));
256+
#else
252257
return *reinterpret_cast<value_type*>(std::addressof(m_value));
258+
#endif
253259
}
254260

255261
const value_type& value() const noexcept {
256262
tsl_rh_assert(!empty());
263+
#if defined(__cplusplus) && __cplusplus >= 201703L
264+
return *std::launder(
265+
reinterpret_cast<const value_type*>(std::addressof(m_value)));
266+
#else
257267
return *reinterpret_cast<const value_type*>(std::addressof(m_value));
268+
#endif
258269
}
259270

260271
distance_type dist_from_ideal_bucket() const noexcept {

0 commit comments

Comments
 (0)