Skip to content

Commit a8fb4fe

Browse files
committed
Improve explicit constructor to allow static_cast according to allocator::pointer requirements:
- Allow only conversion from void - Since conversion from void is address-preserving, use the optimized offset calculation
1 parent 8cc538b commit a8fb4fe

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

include/boost/interprocess/offset_ptr.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,19 @@ class offset_ptr
375375
: internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this))
376376
{}
377377

378-
//!Constructor from other offset_ptr. Only takes part in overload resolution
379-
//!if PointedType* is constructible from T2* other than via a conversion (e.g. cast to a derived class). Never throws.
378+
//!Constructor from other offset_ptr available so that static_cast<> works according to Allocator::pointer requirements:
379+
//! static_cast<pointer>(void_pointer()) + static_cast<const_pointer>(const_void_pointer())
380+
//!Discouraged for any other conversion, static_pointer_cast is the way for downcasts and other static_cast-like conversions.
380381
template<class T2>
381382
BOOST_INTERPROCESS_FORCEINLINE explicit offset_ptr(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
382383
#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
383-
, typename ipcdetail::enable_if_c<
384+
, typename ipcdetail::enable_if_c< ipcdetail::is_cv_same<T2, void>::value && //Allow only void to something casts for static_cast
384385
!::boost::move_detail::is_convertible<T2*, PointedType*>::value &&
385386
ipcdetail::is_ptr_constructible<T2*, PointedType*>::value
386387
>::type * = 0
387388
#endif
388-
) BOOST_NOEXCEPT
389-
: internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this))
389+
) BOOST_NOEXCEPT //void -> T conversion is address-preserving, so take advantage of that
390+
: internal(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.get_offset()))
390391
{}
391392

392393
#endif

0 commit comments

Comments
 (0)