News for hackers. Stuff that matters.
Re:C++ Programming Questions to Ask on Interview (2017) by banachtarski on Friday October 19, 2018 @01:01AM (#18254346)
I think relying on the destructor of the moved-from object running feels idiomatic to me. That's what the destructor is there for after all.
Re:C++ Programming Questions to Ask on Interview (2017) by olliej on Friday October 19, 2018 @01:42AM (#18254444)
The destructor deleting its /own/ data. But here we are moving a bunch of the fields (and so clearing them) but swapping one of them. That seems like bad coding, and doesn’t match any idiom I’ve seen :-/
Re:C++ Programming Questions to Ask on Interview (2017) by archi42 on Friday October 19, 2018 @02:26AM (#18254578)
I wasn't too sure on some of the easy questions, but this swap... Oh god, my "someday, this is going to shoot someone in the foot"-sense started to tingle.
Re:C++ Programming Questions to Ask on Interview (2017) by lemmyg on Friday October 19, 2018 @05:25AM (#18255368)
Move assignment has nothing to do with 'clearing fields' or whatever it's that the moved-to value steals what it can and leaves the moved from value in a valid but unspecified state. Notably, you should still be able to assign-to and destroy a moved-from value.

It's an invariant of the 'DirectorySearchResult' class that query is non-null, as the destructor deletes it without checking it for nullity. The most straight-forward and efficient way to achieve both the stealing and the valid state is to swap the pointers.

Re:C++ Programming Questions to Ask on Interview (2017) by jemfinch on Friday October 19, 2018 @08:35AM (#18256255)
> It's an invariant of the 'DirectorySearchResult' class that query is non-null, as the destructor deletes it without checking it for nullity.

Deleting a null pointer is a fully specified no-op in C++.

Re:C++ Programming Questions to Ask on Interview (2017) by lemmyg on Friday October 19, 2018 @02:17PM (#18258764)
That's true. And I see the other example answers they have written do assume the pointer can be null, although the original class makes no indication of this.

To make the question clearer they should have added a function on the original class that checks (or doesn't check) the pointer before dereferencing it, thereby clarifying whether query being null is valid or not.

Re:C++ Programming Questions to Ask on Interview (2017) by jcelerier on Friday October 19, 2018 @02:21AM (#18254561)
for vectors it makes std::move a very fast operation
Re:C++ Programming Questions to Ask on Interview (2017) by AstralStorm on Friday October 19, 2018 @02:27AM (#18254583)
Vector has a good move assignment which also works. Why swap when you can move?

Swap is additionally risky because someone can ADL specialize it for their own purposes. (which is the point) And suddenly your move assignment is broken.

So people work around it by adding using std::swap clause there...

And then you're calling all those destructors.

Re:C++ Programming Questions to Ask on Interview (2017) by olliej on Friday October 19, 2018 @02:30AM (#18254601)
std::move on vector zeroes out the source element array.

e.g. (pseudo-ish code)

operator=(vector&& foo) {

delete [] elements;

elements = foo.elements;

foo.elements = nullptr;

}

what the example code they have as an answer does is something morally equivalent to this:

operator=(vector&& foo) {

swap(elements, foo.elements);'

}

relying on the knowledge that because it's being moved the ~vector is going to be called immediately afterwards.

This strikes me as being "clever" in the "potentially confusing code, for no real reason" sense

Re:C++ Programming Questions to Ask on Interview (2017) by gpderetta on Friday October 19, 2018 @04:30AM (#18255082)
actually that move assignment operator is not ideal. The foo parameter won't be destroyed immediately but only at the end of whatever scope (if any) it was at the call site.

The idiomatic copy and swap is like this:

T& operator =(T x) // note pass by value { std::swap(this, x); return this; }

This acts both as a normal and move assignment operator (if T is not copy constructible);

edit: how do you get HN to show asterisks? There should be one before each 'this'

Re:C++ Programming Questions to Ask on Interview (2017) by arh68 on Friday October 19, 2018 @07:04AM (#18255764)
Prefix each line with 4 spaces:

    T& operator =(T x) // note pass by value
    { std::swap(*this, x); return *this; }