From: Tianjiao Yin Date: Tue, 18 Mar 2014 00:33:48 +0000 (-0700) Subject: Add constexpr specifier for small_vector::max_size X-Git-Tag: v0.22.0~639 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=00162667c58a92d29a3af16bb11664da337a3c49;hp=4d9662580cdcc559d9a557a9333e28bf311dd3ca 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 --- diff --git a/folly/small_vector.h b/folly/small_vector.h index 5fe9bd26..94f5b601 100644 --- a/folly/small_vector.h +++ b/folly/small_vector.h @@ -228,7 +228,7 @@ namespace detail { IntegralSizePolicy() : size_(0) {} protected: - std::size_t policyMaxSize() const { + static constexpr std::size_t policyMaxSize() { return SizeType(~kExternMask); } @@ -282,7 +282,7 @@ namespace detail { protected: static bool const kShouldUseHeap = ShouldUseHeap; - std::size_t policyMaxSize() const { + static constexpr std::size_t policyMaxSize() { return SizeType(~(SizeType(1) << kLockBit | kExternMask)); } @@ -529,9 +529,9 @@ public: 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 - : this->policyMaxSize(); + : BaseType::policyMaxSize(); } size_type size() const { return this->doSize(); } diff --git a/folly/test/small_vector_test.cpp b/folly/test/small_vector_test.cpp index 200257b7..57b84a4e 100644 --- a/folly/test/small_vector_test.cpp +++ b/folly/test/small_vector_test.cpp @@ -529,7 +529,7 @@ TEST(small_vector, NoHeap) { std::size_t,folly::small_vector_policy::NoHeap> Vector; 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) { v.push_back(folly::to(i)); @@ -546,7 +546,7 @@ TEST(small_vector, NoHeap) { // Check max_size works right with various policy combinations. folly::small_vector v2; - EXPECT_EQ(v2.max_size(), 32); + static_assert(v2.max_size() == 32, "max_size is incorrect"); folly::small_vector v3; EXPECT_EQ(v3.max_size(), (1ul << 30) - 1); folly::small_vector v4; @@ -558,7 +558,8 @@ TEST(small_vector, NoHeap) { * pointer. */ folly::small_vector notsosmall; - EXPECT_EQ(notsosmall.max_size(), sizeof(char*)); + static_assert(notsosmall.max_size() == sizeof(char*), + "max_size is incorrect"); caught = false; try { notsosmall.push_back(12);