added noexcept qualifier to Char ctor, and made notes on exception

safety
This commit is contained in:
Kjistóf 2016-10-29 13:37:03 +02:00
parent 2e05bcb2e6
commit f284a9a471
3 changed files with 6 additions and 1 deletions

View File

@ -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

2
Char.h
View File

@ -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);
};

View File

@ -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