TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Anatomy of the Assignment Operator (1997)

16 pointsby yuashizukiabout 10 years ago

4 comments

conistonwaterabout 10 years ago
Um, what&#x27;s wrong with the simpler<p><pre><code> if (this == &amp;that) return *this; TFoo tmp(that); swap(tmp); return *this; </code></pre> (Assume swap does not throw, it&#x27;s trivial to implement because swapping pointers wouldn&#x27;t throw.) This way the constructor of tmp takes care of destroying partially-constructed objects and you don&#x27;t need to do it by hand. It seems like the clean up code just repeats what the compiler would automatically generate for you in a constructor.<p>[This is from 1997, btw.]
评论 #9140810 未加载
pdpiabout 10 years ago
It&#x27;s an oft-quoted fact that Rust is meant to take stuff that good C&#x2F;C++ programmers already do and build it into the language. If you weren&#x27;t convinced of this before reading this post, then pay close attention to how important the author considers pointer ownership to be.
mainlandabout 10 years ago
The first &quot;correct&quot; solution will leak memory if the call to TSuperFoo::operator= throws an exception.
dietricheppabout 10 years ago
And this is why we use C++11 wherever we can.
评论 #9140889 未加载