Commit efedce93 authored by Chad Austin's avatar Chad Austin Committed by Facebook GitHub Bot

Require fbstring be used with std::allocator

Summary:
basic_fbstring provides an allocator template paramater for
compatibility with basic_string, but ignores it. Assert at compile
time that a custom allocator isn't used.

This would have prevented the regression introduced in D22970418 (https://github.com/facebook/folly/commit/abb4ece62120ff32aaf28dc4bbfdc6a2f2b41f78) and
fixed in D23035304 (https://github.com/facebook/folly/commit/8b650246220717f27046593b5819fbaa60427bcb).

Reviewed By: luciang

Differential Revision: D23144806

fbshipit-source-id: c541f274e97736b9408651a66088bcbb78025539
parent 63dc5615
...@@ -975,6 +975,10 @@ template < ...@@ -975,6 +975,10 @@ template <
class A = std::allocator<E>, class A = std::allocator<E>,
class Storage = fbstring_core<E>> class Storage = fbstring_core<E>>
class basic_fbstring { class basic_fbstring {
static_assert(
std::is_same<A, std::allocator<E>>::value,
"fbstring ignores custom allocators");
template <typename Ex, typename... Args> template <typename Ex, typename... Args>
FOLLY_ALWAYS_INLINE static void enforce(bool condition, Args&&... args) { FOLLY_ALWAYS_INLINE static void enforce(bool condition, Args&&... args) {
if (!condition) { if (!condition) {
......
...@@ -1498,11 +1498,6 @@ struct TestStructDefaultAllocator { ...@@ -1498,11 +1498,6 @@ struct TestStructDefaultAllocator {
folly::basic_fbstring<char> stringMember; folly::basic_fbstring<char> stringMember;
}; };
template <class A>
struct TestStructWithAllocator {
folly::basic_fbstring<char, std::char_traits<char>, A> stringMember;
};
std::atomic<size_t> allocatorConstructedCount(0); std::atomic<size_t> allocatorConstructedCount(0);
struct TestStructStringAllocator : std::allocator<char> { struct TestStructStringAllocator : std::allocator<char> {
TestStructStringAllocator() { TestStructStringAllocator() {
...@@ -1517,13 +1512,6 @@ TEST(FBStringCtorTest, DefaultInitStructDefaultAlloc) { ...@@ -1517,13 +1512,6 @@ TEST(FBStringCtorTest, DefaultInitStructDefaultAlloc) {
EXPECT_TRUE(t1.stringMember.empty()); EXPECT_TRUE(t1.stringMember.empty());
} }
TEST(FBStringCtorTest, DefaultInitStructAlloc) {
EXPECT_EQ(allocatorConstructedCount.load(), 0);
TestStructWithAllocator<TestStructStringAllocator> t2;
EXPECT_TRUE(t2.stringMember.empty());
EXPECT_EQ(allocatorConstructedCount.load(), 1);
}
TEST(FBStringCtorTest, NullZeroConstruction) { TEST(FBStringCtorTest, NullZeroConstruction) {
char* p = nullptr; char* p = nullptr;
int n = 0; int n = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment