mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-03-18 22:29:42 +01:00
Fix ADL Participation for swap
in 105-copy-swap.md (#4925)
This commit is contained in:
parent
7c5b49876a
commit
c1fb58dab7
@ -16,14 +16,14 @@ class String {
|
||||
|
||||
String(const String& other);
|
||||
|
||||
void swap(String& other) {
|
||||
friend void swap(String& first, String& second) {
|
||||
using std::swap; // for arguments-dependent lookup (ADL)
|
||||
swap(size_, other.size_);
|
||||
swap(buffer_, other.buffer_);
|
||||
swap(first.size_, second.size_);
|
||||
swap(first.buffer_, second.buffer_);
|
||||
}
|
||||
|
||||
String& operator=(String other) {
|
||||
swap(other);
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
@ -34,4 +34,4 @@ Using the copy-swap idiom:
|
||||
- The left-hand side object's contents are swapped with the temporary copy.
|
||||
- The temporary copy is destroyed, releasing any resources that were previously held by the left-hand side object.
|
||||
|
||||
This approach simplifies the implementation and provides strong exception safety, while reusing the copy constructor and destructor code.
|
||||
This approach simplifies the implementation and provides strong exception safety, while reusing the copy constructor and destructor code.
|
||||
|
Loading…
x
Reference in New Issue
Block a user