From: Tudor Bosman Date: Tue, 24 Sep 2013 23:49:24 +0000 (-0700) Subject: Add noexcept specification for move ctor, move assignment, dtor in fbstring X-Git-Tag: v0.22.0~866 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=cca262b3a51a2a49749c3a0afd303f7a54b0837d;p=folly.git Add noexcept specification for move ctor, move assignment, dtor in fbstring Test Plan: fbstring_test, test added Reviewed By: ngbronson@fb.com FB internal diff: D983278 --- diff --git a/folly/FBString.h b/folly/FBString.h index e19943ab..c99bb4ad 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -305,7 +305,7 @@ private: */ template class fbstring_core { public: - fbstring_core() { + fbstring_core() noexcept { // Only initialize the tag, will set the MSBs (i.e. the small // string size) to zero too ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char))); @@ -359,7 +359,7 @@ public: assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0); } - fbstring_core(fbstring_core&& goner) { + fbstring_core(fbstring_core&& goner) noexcept { if (goner.category() == isSmall) { // Just copy, leave the goner in peace new(this) fbstring_core(goner.small_, goner.smallSize()); @@ -428,7 +428,7 @@ public: assert(memcmp(this->data(), data, size * sizeof(Char)) == 0); } - ~fbstring_core() { + ~fbstring_core() noexcept { auto const c = category(); if (c == isSmall) { return; @@ -1014,7 +1014,7 @@ private: public: // C++11 21.4.2 construct/copy/destroy - explicit basic_fbstring(const A& a = A()) { + explicit basic_fbstring(const A& a = A()) noexcept { } basic_fbstring(const basic_fbstring& str) @@ -1022,7 +1022,8 @@ public: } // Move constructor - basic_fbstring(basic_fbstring&& goner) : store_(std::move(goner.store_)) { + basic_fbstring(basic_fbstring&& goner) noexcept + : store_(std::move(goner.store_)) { } #ifndef _LIBSTDCXX_FBSTRING @@ -1080,7 +1081,7 @@ public: assign(il.begin(), il.end()); } - ~basic_fbstring() { + ~basic_fbstring() noexcept { } basic_fbstring& operator=(const basic_fbstring& lhs) { @@ -1106,7 +1107,7 @@ public: } // Move assignment - basic_fbstring& operator=(basic_fbstring&& goner) { + basic_fbstring& operator=(basic_fbstring&& goner) noexcept { if (FBSTRING_UNLIKELY(&goner == this)) { // Compatibility with std::basic_string<>, // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support. diff --git a/folly/test/FBStringTest.cpp b/folly/test/FBStringTest.cpp index ede69e6a..36c8fe55 100644 --- a/folly/test/FBStringTest.cpp +++ b/folly/test/FBStringTest.cpp @@ -30,6 +30,7 @@ #include #include "folly/Foreach.h" +#include "folly/Portability.h" #include "folly/Random.h" #include "folly/Conv.h" @@ -1162,6 +1163,19 @@ TEST(FBString, testFrontBack) { EXPECT_EQ(str, "HellO"); } +TEST(FBString, noexcept) { + EXPECT_TRUE(noexcept(fbstring())); + // std::move is not marked noexcept in gcc 4.6, sigh +#if __GNUC_PREREQ(4, 7) + fbstring x; + EXPECT_FALSE(noexcept(fbstring(x))); + EXPECT_TRUE(noexcept(fbstring(std::move(x)))); + fbstring y; + EXPECT_FALSE(noexcept(y = x)); + EXPECT_TRUE(noexcept(y = std::move(x))); +#endif +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true);