Skip to content

Commit 25331ba

Browse files
mszabo-wikiafacebook-github-bot
authored andcommitted
Fix ill-formed req::Allocator rebind (#9640)
Summary: Given an Allocator A<T> and some cv-unqualified object type U, let B = A::template rebind<U>::other. The C++ standard stipulates[1] that in this case, B::template rebind<T>::other must be A for any U. HPHP::req::Allocator does not satisfy this constraint at present, but this does not fail compilation at all times because the constraint is inconsistently enforced by different standard library implementations. Consider the simplified reproducer in https://godbolt.org/z/9b19z4Yz8. If using libstdc++, the code won't compile with any libstdc++ > 12, because alloc_traits.h enforces this requirement. If using libc++, the code will compile, but uncomment the std::vector variable definition in main() and it will trigger libc++'s own check for this constraint and fail. So, make req::Allocator::rebind conform to the standard by having it return the original allocator in this case. [1] https://en.cppreference.com/w/cpp/named_req/Allocator.html Pull Request resolved: #9640 Reviewed By: ricklavoie Differential Revision: D83148440 fbshipit-source-id: 63aa13ee82c67d377d373693fd0e3a7b04c1350a
1 parent 8493090 commit 25331ba

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

hphp/runtime/base/req-malloc.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,18 @@ struct Allocator {
170170

171171
template <class U>
172172
struct rebind {
173-
using other = Allocator<
174-
U,
175-
typename action_helper<
176-
typename std::remove_const<
177-
typename std::remove_pointer<U>::type
178-
>::type, Action
179-
>::type
180-
>;
173+
using other = std::conditional_t<
174+
std::is_same<U, T>::value,
175+
Allocator<T, Action>,
176+
Allocator<
177+
U,
178+
typename action_helper<
179+
typename std::remove_const<
180+
typename std::remove_pointer<U>::type
181+
>::type, Action
182+
>::type
183+
>
184+
>;
181185
};
182186

183187
pointer address(reference value) {
@@ -275,4 +279,3 @@ template<class T> T* calloc_raw_array(size_t count) {
275279
////////////////////////////////////////////////////////////////////////////////
276280

277281
}
278-

0 commit comments

Comments
 (0)