diff --git a/Char.cpp b/Char.cpp index a3ef41c..ea26a4d 100644 --- a/Char.cpp +++ b/Char.cpp @@ -6,6 +6,7 @@ Char::operator char() const { return _string._str->operator[](_index); } // copy-on-write magic happens here +// provides strong exception guarantee Char& Char::operator=(char other) { auto data = new char[_string.size() + 1]; // space for old string & null-terminator diff --git a/Char.h b/Char.h index 818c0e9..bcdb308 100644 --- a/Char.h +++ b/Char.h @@ -17,7 +17,7 @@ private: public: - explicit Char(size_t index, String& string) :_index(index), _string(string) {} + explicit Char(size_t index, String& string) noexcept :_index(index), _string(string) {} operator char() const; Char& operator=(char other); }; \ No newline at end of file diff --git a/String.cpp b/String.cpp index 5785ae5..757078d 100644 --- a/String.cpp +++ b/String.cpp @@ -24,6 +24,7 @@ String::String(const String& other) noexcept } // copy&move-assignment operator with copy-and-swap +// provides strong exception guarantee String& String::operator=(String other) // take parameter by value: can bind to copy and move-assignment { // & the compiler can optimize copying swap(*this, other); @@ -57,6 +58,7 @@ String String::operator+(const String& rhs) const return ret; } +// provides strong exception guarantee String& String::operator+=(const String& rhs) { auto data = this->copy_and_concat(rhs); @@ -87,6 +89,7 @@ String String::operator+(char rhs) const return ret; } +// provides strong exception guarantee String& String::operator+=(char rhs) { auto data = this->copy_and_concat(rhs); @@ -143,6 +146,7 @@ const char* String::c_str() const throw std::runtime_error("You cannot get the content of uninitialized Strings!"); } +// provides strong exception guarantee std::istream& operator>>(std::istream& is, String& str) { constexpr size_t BUFSIZE = 50; // set some reasonable buffer size