Skip to content

Commit ec9317e

Browse files
Move past member call on object past it's lifetime
1 parent c04441e commit ec9317e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

libcxx/include/__tree

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,11 @@ struct __tree_key_value_types<__value_type<_Key, _Tp> > {
543543
}
544544

545545
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static __container_value_type* __get_ptr(__node_value_type& __n) {
546-
return std::addressof(__n.__get_value());
546+
// __n's lifetime has not begun, so calling __get_value is wrong
547+
548+
// return std::addressof(__n.__get_value());
549+
550+
return __node_value_type::__get_address_of_value(__n);
547551
}
548552

549553
_LIBCPP_HIDE_FROM_ABI static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) { return __v.__move(); }
@@ -658,6 +662,7 @@ public:
658662
__node_value_type __value_;
659663

660664
_LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
665+
// _LIBCPP_HIDE_FROM_ABI static _Tp& __get_value_static(__tree_node * foo) { return (foo->__value_); }
661666

662667
~__tree_node() = delete;
663668
__tree_node(__tree_node const&) = delete;
@@ -1816,8 +1821,9 @@ __tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&&... __args) {
18161821

18171822
template <class _Tp, class _Compare, class _Allocator>
18181823
template <class... _Args>
1824+
_LIBCPP_CONSTEXPR_SINCE_CXX26
18191825
pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1820-
_LIBCPP_CONSTEXPR_SINCE_CXX26 __tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args) {
1826+
__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args) {
18211827
__node_holder __h = __construct_node(std::forward<_Args>(__args)...);
18221828
__parent_pointer __parent;
18231829
__node_base_pointer& __child = __find_equal(__parent, __h->__value_);

libcxx/include/map

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,17 @@ public:
790790
# endif
791791
}
792792

793+
794+
/*
795+
Use this helper when the lifetime of __v may not have begun,
796+
so calling __v.__get_value() is not allowed
797+
as per:
798+
`note: member call on object outside its lifetime is not allowed in a constant expression`
799+
*/
800+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static value_type* __get_address_of_value(__value_type & __v) {
801+
return std::addressof(__v.__cc_);
802+
}
803+
793804
_LIBCPP_HIDE_FROM_ABI __nc_ref_pair_type __ref() {
794805
value_type& __v = __get_value();
795806
return __nc_ref_pair_type(const_cast<key_type&>(__v.first), __v.second);
@@ -837,6 +848,13 @@ public:
837848
_LIBCPP_HIDE_FROM_ABI value_type& __get_value() { return __cc_; }
838849
_LIBCPP_HIDE_FROM_ABI const value_type& __get_value() const { return __cc_; }
839850

851+
852+
// TODO: maybe needed
853+
// _LIBCPP_HIDE_FROM_ABI static value_type* __get_address_of_value(__value_type const& __v) {
854+
// return std::addressof(__v.__cc_);
855+
// }
856+
857+
840858
__value_type() = delete;
841859
__value_type(__value_type const&) = delete;
842860
__value_type& operator=(__value_type const&) = delete;

0 commit comments

Comments
 (0)