From c1fb58dab7545d111eb73fa01a1615bcc08e4892 Mon Sep 17 00:00:00 2001 From: Ali Ashkani Nia Date: Sat, 11 May 2024 00:39:49 +0330 Subject: [PATCH] Fix ADL Participation for `swap` in 105-copy-swap.md (#4925) --- .../roadmaps/cpp/content/112-idioms/105-copy-swap.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/data/roadmaps/cpp/content/112-idioms/105-copy-swap.md b/src/data/roadmaps/cpp/content/112-idioms/105-copy-swap.md index 77950bdf0..27c0ba33c 100644 --- a/src/data/roadmaps/cpp/content/112-idioms/105-copy-swap.md +++ b/src/data/roadmaps/cpp/content/112-idioms/105-copy-swap.md @@ -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. \ No newline at end of file +This approach simplifies the implementation and provides strong exception safety, while reusing the copy constructor and destructor code.