Add constexpr specifier for small_vector::max_size
authorTianjiao Yin <ytj@fb.com>
Tue, 18 Mar 2014 00:33:48 +0000 (17:33 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 18 Mar 2014 17:02:24 +0000 (10:02 -0700)
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

folly/small_vector.h
folly/test/small_vector_test.cpp

index 5fe9bd26226b0dbf6beae2dadf75f22d2a86f4bb..94f5b601d5492bccb4bc9184ff159e2d22363850 100644 (file)
@@ -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(); }
index 200257b77e5c06f03fbdd3f6d62b68bfcbd060bd..57b84a4efa345fccbc72c7156d0d736d359fd7ad 100644 (file)
@@ -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<std::string>(i));
@@ -546,7 +546,7 @@ TEST(small_vector, NoHeap) {
 
   // Check max_size works right with various policy combinations.
   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;
   EXPECT_EQ(v3.max_size(), (1ul << 30) - 1);
   folly::small_vector<std::string,32,uint32_t> v4;
@@ -558,7 +558,8 @@ TEST(small_vector, NoHeap) {
    * pointer.
    */
   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;
   try {
     notsosmall.push_back(12);