|
|
|
|
|
by quicknir
3997 days ago
|
|
Actually, that's not true. Here's what I wrote: "By stealing the string, we put foo in an invalid state". foo is an object of the SetStringer class. It puts it an in invalid state because it no longer maintains its own invariants. The whole point of this article is about how you can move member variables for efficiency, while ensuring the owning object is left in a valid state (as move/rvalue semantics require). Wanting to be able to move a member variable is no stranger than wanting to move the object itself; in that sense an rvalue ref overloaded getter is no stranger than move construction/assignment, it's just more rare because it's less used. |
|
While I am not a C++ day-developer nor guru, from what I have seen and written of modern C++, the "move member variables" is not "good", in any sense of the word, stemming from the reasons put forth by many a comment.
If you ever want to move a member variable, I would highly recommend that you re-think your design. Ask questions such as "why do I want this?" and "is it really more efficient?". In some cases, where perf is really that important, then yea, go for it. However, just because something can be done, does not mean that it should be done.
While I still disagree with your const & return of the original string, I threw together an example showing the same exact API you proposed, but without moving member variables:
https://gist.github.com/wallstop/4d80fba9b1d15da64488
Depending on method usage, this may or may not invoke the cost of an extra string creation (+ extra on move method, - for continual access of cached string)