Last active
November 1, 2015 11:30
-
-
Save Zulan/1d80b795afb357e94b73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory> | |
struct node | |
{ | |
int value; | |
node* next; | |
}; | |
struct smart_node | |
{ | |
int value; | |
std::unique_ptr<smart_node> next; | |
}; | |
void clear(node& root) | |
{ | |
node* current = root.next; | |
root.next = nullptr; | |
while (current != nullptr) { | |
node* next = current->next; | |
delete current; | |
current = next; | |
} | |
} | |
void clear(smart_node& root) | |
{ | |
std::unique_ptr<smart_node> current = std::move(root.next); | |
while (current) { | |
std::unique_ptr<smart_node> next = std::move(current->next); | |
current = std::move(next); | |
} | |
} | |
// NOTE: I am not 100% sure if the following would be generically correct in case of non-standard delters | |
// current = std::move(current->next); | |
// The briefest alternative would be | |
// current.reset(current->next.release()); | |
// With the default deleter all three options should produce the same assembler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment