Skip to content

Commit

Permalink
Merge pull request #714 from dtolnay/rvalue-assign
Browse files Browse the repository at this point in the history
Disallow assignment to rvalue
  • Loading branch information
dtolnay authored Feb 12, 2021
2 parents 19b488c + 63bec40 commit d3cb114
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions include/cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class String final {
String(const char *);
String(const char *, std::size_t);

String &operator=(const String &) noexcept;
String &operator=(String &&) noexcept;
String &operator=(const String &) & noexcept;
String &operator=(String &&) & noexcept;

explicit operator std::string() const;

Expand Down Expand Up @@ -97,7 +97,7 @@ class Str final {
Str(const char *);
Str(const char *, std::size_t);

Str &operator=(const Str &) noexcept = default;
Str &operator=(const Str &) & noexcept = default;

explicit operator std::string() const;

Expand Down Expand Up @@ -144,8 +144,8 @@ template <>
struct copy_assignable_if<false> {
copy_assignable_if() noexcept = default;
copy_assignable_if(const copy_assignable_if &) noexcept = default;
copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
copy_assignable_if &operator=(const copy_assignable_if &) & noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
};
} // namespace detail

Expand All @@ -159,8 +159,8 @@ class Slice final
Slice() noexcept;
Slice(T *, std::size_t count) noexcept;

Slice &operator=(const Slice<T> &) noexcept = default;
Slice &operator=(Slice<T> &&) noexcept = default;
Slice &operator=(const Slice<T> &) & noexcept = default;
Slice &operator=(Slice<T> &&) & noexcept = default;

T *data() const noexcept;
std::size_t size() const noexcept;
Expand Down Expand Up @@ -248,7 +248,7 @@ class Box final {
explicit Box(const T &);
explicit Box(T &&);

Box &operator=(Box &&) noexcept;
Box &operator=(Box &&) & noexcept;

const T *operator->() const noexcept;
const T &operator*() const noexcept;
Expand Down Expand Up @@ -293,8 +293,8 @@ class Vec final {
Vec(Vec &&) noexcept;
~Vec() noexcept;

Vec &operator=(Vec &&) noexcept;
Vec &operator=(const Vec &);
Vec &operator=(Vec &&) & noexcept;
Vec &operator=(const Vec &) &;

std::size_t size() const noexcept;
bool empty() const noexcept;
Expand Down Expand Up @@ -371,8 +371,8 @@ class Error final : public std::exception {
Error(Error &&) noexcept;
~Error() noexcept override;

Error &operator=(const Error &);
Error &operator=(Error &&) noexcept;
Error &operator=(const Error &) &;
Error &operator=(Error &&) & noexcept;

const char *what() const noexcept override;

Expand Down Expand Up @@ -735,7 +735,7 @@ Box<T>::~Box() noexcept {
}

template <typename T>
Box<T> &Box<T>::operator=(Box &&other) noexcept {
Box<T> &Box<T>::operator=(Box &&other) & noexcept {
if (this->ptr) {
this->drop();
}
Expand Down Expand Up @@ -823,15 +823,15 @@ Vec<T>::~Vec() noexcept {
}

template <typename T>
Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
Vec<T> &Vec<T>::operator=(Vec &&other) & noexcept {
this->drop();
this->repr = other.repr;
new (&other) Vec();
return *this;
}

template <typename T>
Vec<T> &Vec<T>::operator=(const Vec &other) {
Vec<T> &Vec<T>::operator=(const Vec &other) & {
if (this != &other) {
this->drop();
new (this) Vec(other);
Expand Down
8 changes: 4 additions & 4 deletions src/cxx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ String::String(const char *s, std::size_t len) {
len);
}

String &String::operator=(const String &other) noexcept {
String &String::operator=(const String &other) & noexcept {
if (this != &other) {
cxxbridge1$string$drop(this);
cxxbridge1$string$clone(this, other);
}
return *this;
}

String &String::operator=(String &&other) noexcept {
String &String::operator=(String &&other) & noexcept {
cxxbridge1$string$drop(this);
this->repr = other.repr;
cxxbridge1$string$new(&other);
Expand Down Expand Up @@ -376,7 +376,7 @@ Error::Error(Error &&other) noexcept

Error::~Error() noexcept { delete[] this->msg; }

Error &Error::operator=(const Error &other) {
Error &Error::operator=(const Error &other) & {
if (this != &other) {
std::exception::operator=(other);
delete[] this->msg;
Expand All @@ -389,7 +389,7 @@ Error &Error::operator=(const Error &other) {
return *this;
}

Error &Error::operator=(Error &&other) noexcept {
Error &Error::operator=(Error &&other) & noexcept {
std::exception::operator=(std::move(other));
this->msg = other.msg;
this->len = other.len;
Expand Down

0 comments on commit d3cb114

Please sign in to comment.