std::move'ing from a member variable

175 views
Skip to first unread message

Zentaro Kavanagh

unread,
Apr 5, 2022, 12:18:41 PM4/5/22
to cxx
Is it still correct that the only thing we should expect from the moved-from type is that it is valid and safely destructible? I think we have something that checks for use-after-move which I think works for local variables, which implies we should not be moving from a member unless we ca n be sure it is never used again.

In the case where that can't be validated, or where we explicitly want to reuse for example a member vector full of move only objects, is swap the recommended way to do this?

eg.
std::vector ret; member_.swap(ret); return ret;

dan...@chromium.org

unread,
Apr 5, 2022, 12:22:21 PM4/5/22
to Zentaro Kavanagh, cxx
You can also reset the variable after move:

std::vector ret = std::move(member_);
member = std::vector<T>();
return ret;

Your understanding that we should not use-after-move is correct (with a few exceptions like smart pointers which allow it)

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CABO%2BUTqJfUVFwbbwNmsDg02Nv-AExoWg5p-Hf4BEYZdHcTHt1Q%40mail.gmail.com.

Roland McGrath

unread,
Apr 5, 2022, 2:07:36 PM4/5/22
to Dana Jansens, Zentaro Kavanagh, cxx
There is also `std::exchange`: `return std::exchange(member_, {});` is often the most concise thing.


Lei Zhang

unread,
Apr 5, 2022, 7:53:39 PM4/5/22
to dan...@chromium.org, Zentaro Kavanagh, cxx
For the vector case, item 8 on [1] says `member_` is guaranteed to be
empty. Should be fine to just return std::move(member_), right?

[1] https://en.cppreference.com/w/cpp/container/vector/vector

On Tue, Apr 5, 2022 at 9:22 AM <dan...@chromium.org> wrote:
>
> To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaSyUP%3D4FXxHcC%3DuG_eeJ3yfb95RyyFuFPQpd__0XfZJEw%40mail.gmail.com.

dan...@chromium.org

unread,
Apr 6, 2022, 9:29:09 AM4/6/22
to Lei Zhang, Zentaro Kavanagh, cxx
On Tue, Apr 5, 2022 at 7:53 PM Lei Zhang <the...@chromium.org> wrote:
For the vector case, item 8 on [1] says `member_` is guaranteed to be 
empty. Should be fine to just return std::move(member_), right?

Ah, looks right yeah, for vector. I didn't recall that vector defines that.

Peter Kasting

unread,
Apr 6, 2022, 9:45:22 AM4/6/22
to dan...@chromium.org, Lei Zhang, Zentaro Kavanagh, cxx
On Wed, Apr 6, 2022 at 6:29 AM <dan...@chromium.org> wrote:
On Tue, Apr 5, 2022 at 7:53 PM Lei Zhang <the...@chromium.org> wrote:
For the vector case, item 8 on [1] says `member_` is guaranteed to be 
empty. Should be fine to just return std::move(member_), right?

Ah, looks right yeah, for vector. I didn't recall that vector defines that.

Neither will anyone who reads the code.  It will look like a bug.

 I'd go the std::exchange() route myself.

PK

Gabriel Charette

unread,
May 2, 2022, 2:09:42 PM5/2/22
to dan...@chromium.org, Lei Zhang, Zentaro Kavanagh, cxx
I have a stalled proposal to do this automatically for member variables via base::ResetOnMove<T>.
This would allow operator=(Foo&&) = default; instead of forcing the explicit declaration of all move operations because a single member must be reset (default constructed) after move.

Reply all
Reply to author
Forward
0 new messages