Commit 00162667 authored by Tianjiao Yin's avatar Tianjiao Yin Committed by Dave Watson

Add constexpr specifier for small_vector::max_size

Summary: Now we could use small_vector::max_size in constant expressions

Test Plan: unittest

Reviewed By: delong.j@fb.com

FB internal diff: D1224274
parent 4d966258
...@@ -228,7 +228,7 @@ namespace detail { ...@@ -228,7 +228,7 @@ namespace detail {
IntegralSizePolicy() : size_(0) {} IntegralSizePolicy() : size_(0) {}
protected: protected:
std::size_t policyMaxSize() const { static constexpr std::size_t policyMaxSize() {
return SizeType(~kExternMask); return SizeType(~kExternMask);
} }
...@@ -282,7 +282,7 @@ namespace detail { ...@@ -282,7 +282,7 @@ namespace detail {
protected: protected:
static bool const kShouldUseHeap = ShouldUseHeap; static bool const kShouldUseHeap = ShouldUseHeap;
std::size_t policyMaxSize() const { static constexpr std::size_t policyMaxSize() {
return SizeType(~(SizeType(1) << kLockBit | kExternMask)); return SizeType(~(SizeType(1) << kLockBit | kExternMask));
} }
...@@ -529,9 +529,9 @@ public: ...@@ -529,9 +529,9 @@ public:
return std::lexicographical_compare(begin(), end(), o.begin(), o.end()); return std::lexicographical_compare(begin(), end(), o.begin(), o.end());
} }
size_type max_size() const { static constexpr size_type max_size() {
return !BaseType::kShouldUseHeap ? MaxInline return !BaseType::kShouldUseHeap ? MaxInline
: this->policyMaxSize(); : BaseType::policyMaxSize();
} }
size_type size() const { return this->doSize(); } size_type size() const { return this->doSize(); }
......
...@@ -529,7 +529,7 @@ TEST(small_vector, NoHeap) { ...@@ -529,7 +529,7 @@ TEST(small_vector, NoHeap) {
std::size_t,folly::small_vector_policy::NoHeap> Vector; std::size_t,folly::small_vector_policy::NoHeap> Vector;
Vector v; Vector v;
EXPECT_EQ(v.max_size(), 10); static_assert(v.max_size() == 10, "max_size is incorrect");
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
v.push_back(folly::to<std::string>(i)); v.push_back(folly::to<std::string>(i));
...@@ -546,7 +546,7 @@ TEST(small_vector, NoHeap) { ...@@ -546,7 +546,7 @@ TEST(small_vector, NoHeap) {
// Check max_size works right with various policy combinations. // Check max_size works right with various policy combinations.
folly::small_vector<std::string,32,uint32_t,NoHeap,OneBitMutex> v2; folly::small_vector<std::string,32,uint32_t,NoHeap,OneBitMutex> v2;
EXPECT_EQ(v2.max_size(), 32); static_assert(v2.max_size() == 32, "max_size is incorrect");
folly::small_vector<std::string,32,uint32_t,OneBitMutex> v3; folly::small_vector<std::string,32,uint32_t,OneBitMutex> v3;
EXPECT_EQ(v3.max_size(), (1ul << 30) - 1); EXPECT_EQ(v3.max_size(), (1ul << 30) - 1);
folly::small_vector<std::string,32,uint32_t> v4; folly::small_vector<std::string,32,uint32_t> v4;
...@@ -558,7 +558,8 @@ TEST(small_vector, NoHeap) { ...@@ -558,7 +558,8 @@ TEST(small_vector, NoHeap) {
* pointer. * pointer.
*/ */
folly::small_vector<char,1,NoHeap> notsosmall; folly::small_vector<char,1,NoHeap> notsosmall;
EXPECT_EQ(notsosmall.max_size(), sizeof(char*)); static_assert(notsosmall.max_size() == sizeof(char*),
"max_size is incorrect");
caught = false; caught = false;
try { try {
notsosmall.push_back(12); notsosmall.push_back(12);
......
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