Commit cca262b3 authored by Tudor Bosman's avatar Tudor Bosman Committed by Peter Griess

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
parent d3468ea8
...@@ -305,7 +305,7 @@ private: ...@@ -305,7 +305,7 @@ private:
*/ */
template <class Char> class fbstring_core { template <class Char> class fbstring_core {
public: public:
fbstring_core() { fbstring_core() noexcept {
// Only initialize the tag, will set the MSBs (i.e. the small // Only initialize the tag, will set the MSBs (i.e. the small
// string size) to zero too // string size) to zero too
ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char))); ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)));
...@@ -359,7 +359,7 @@ public: ...@@ -359,7 +359,7 @@ public:
assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0); assert(memcmp(data(), rhs.data(), size() * sizeof(Char)) == 0);
} }
fbstring_core(fbstring_core&& goner) { fbstring_core(fbstring_core&& goner) noexcept {
if (goner.category() == isSmall) { if (goner.category() == isSmall) {
// Just copy, leave the goner in peace // Just copy, leave the goner in peace
new(this) fbstring_core(goner.small_, goner.smallSize()); new(this) fbstring_core(goner.small_, goner.smallSize());
...@@ -428,7 +428,7 @@ public: ...@@ -428,7 +428,7 @@ public:
assert(memcmp(this->data(), data, size * sizeof(Char)) == 0); assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
} }
~fbstring_core() { ~fbstring_core() noexcept {
auto const c = category(); auto const c = category();
if (c == isSmall) { if (c == isSmall) {
return; return;
...@@ -1014,7 +1014,7 @@ private: ...@@ -1014,7 +1014,7 @@ private:
public: public:
// C++11 21.4.2 construct/copy/destroy // 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) basic_fbstring(const basic_fbstring& str)
...@@ -1022,7 +1022,8 @@ public: ...@@ -1022,7 +1022,8 @@ public:
} }
// Move constructor // 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 #ifndef _LIBSTDCXX_FBSTRING
...@@ -1080,7 +1081,7 @@ public: ...@@ -1080,7 +1081,7 @@ public:
assign(il.begin(), il.end()); assign(il.begin(), il.end());
} }
~basic_fbstring() { ~basic_fbstring() noexcept {
} }
basic_fbstring& operator=(const basic_fbstring& lhs) { basic_fbstring& operator=(const basic_fbstring& lhs) {
...@@ -1106,7 +1107,7 @@ public: ...@@ -1106,7 +1107,7 @@ public:
} }
// Move assignment // Move assignment
basic_fbstring& operator=(basic_fbstring&& goner) { basic_fbstring& operator=(basic_fbstring&& goner) noexcept {
if (FBSTRING_UNLIKELY(&goner == this)) { if (FBSTRING_UNLIKELY(&goner == this)) {
// Compatibility with std::basic_string<>, // Compatibility with std::basic_string<>,
// C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support. // C++11 21.4.2 [string.cons] / 23 requires self-move-assignment support.
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <gflags/gflags.h> #include <gflags/gflags.h>
#include "folly/Foreach.h" #include "folly/Foreach.h"
#include "folly/Portability.h"
#include "folly/Random.h" #include "folly/Random.h"
#include "folly/Conv.h" #include "folly/Conv.h"
...@@ -1162,6 +1163,19 @@ TEST(FBString, testFrontBack) { ...@@ -1162,6 +1163,19 @@ TEST(FBString, testFrontBack) {
EXPECT_EQ(str, "HellO"); 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) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
......
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