added noexcept qualifier to Char ctor, and made notes on exception
safety
This commit is contained in:
parent
2e05bcb2e6
commit
f284a9a471
1
Char.cpp
1
Char.cpp
@ -6,6 +6,7 @@ Char::operator char() const
|
|||||||
{ return _string._str->operator[](_index); }
|
{ return _string._str->operator[](_index); }
|
||||||
|
|
||||||
// copy-on-write magic happens here
|
// copy-on-write magic happens here
|
||||||
|
// provides strong exception guarantee
|
||||||
Char& Char::operator=(char other)
|
Char& Char::operator=(char other)
|
||||||
{
|
{
|
||||||
auto data = new char[_string.size() + 1]; // space for old string & null-terminator
|
auto data = new char[_string.size() + 1]; // space for old string & null-terminator
|
||||||
|
2
Char.h
2
Char.h
@ -17,7 +17,7 @@ private:
|
|||||||
|
|
||||||
public:
|
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;
|
operator char() const;
|
||||||
Char& operator=(char other);
|
Char& operator=(char other);
|
||||||
};
|
};
|
@ -24,6 +24,7 @@ String::String(const String& other) noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
// copy&move-assignment operator with copy-and-swap
|
// 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
|
String& String::operator=(String other) // take parameter by value: can bind to copy and move-assignment
|
||||||
{ // & the compiler can optimize copying
|
{ // & the compiler can optimize copying
|
||||||
swap(*this, other);
|
swap(*this, other);
|
||||||
@ -57,6 +58,7 @@ String String::operator+(const String& rhs) const
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// provides strong exception guarantee
|
||||||
String& String::operator+=(const String& rhs)
|
String& String::operator+=(const String& rhs)
|
||||||
{
|
{
|
||||||
auto data = this->copy_and_concat(rhs);
|
auto data = this->copy_and_concat(rhs);
|
||||||
@ -87,6 +89,7 @@ String String::operator+(char rhs) const
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// provides strong exception guarantee
|
||||||
String& String::operator+=(char rhs)
|
String& String::operator+=(char rhs)
|
||||||
{
|
{
|
||||||
auto data = this->copy_and_concat(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!");
|
throw std::runtime_error("You cannot get the content of uninitialized Strings!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// provides strong exception guarantee
|
||||||
std::istream& operator>>(std::istream& is, String& str)
|
std::istream& operator>>(std::istream& is, String& str)
|
||||||
{
|
{
|
||||||
constexpr size_t BUFSIZE = 50; // set some reasonable buffer size
|
constexpr size_t BUFSIZE = 50; // set some reasonable buffer size
|
||||||
|
Loading…
Reference in New Issue
Block a user