News for hackers. Stuff that matters.
Re:Modern C++ for C Programmers: part 5 by mikec3010 on Wednesday July 25, 2018 @11:08AM (#17609843)
> The move constructor is the important bit. Its presence tells C++ that this class can not be copied, only moved

Is that true? I thought move ctor enabled move semantics, but if you pass by value the copy ctor was still called (except in places where RVO makes sense). And I thought to disable copying you made the copy ctor = delete.

Re:Modern C++ for C Programmers: part 5 by tlb on Wednesday July 25, 2018 @11:18AM (#17609918)
"The implicitly-declared or defaulted copy constructor for class T is defined as deleted if any of the following conditions are true ... T has a user-defined move constructor or move assignment operator" (since C++11)

https://en.cppreference.com/w/cpp/language/copy_constructor

But there's no harm in explicitly deleting it, and it helps document the intent.

Re:Modern C++ for C Programmers: part 5 by _pmf_ on Wednesday July 25, 2018 @12:36PM (#17610564)
What's the motivation behind this?
Re:Modern C++ for C Programmers: part 5 by tlb on Wednesday July 25, 2018 @12:45PM (#17610654)
The committee probably observed that in most cases where people wanted a move constructor, they needed either no copy constructor or a more complicated one (to handle duplicating a resource.) So creating a default copy constructor (a convenience in the common case of a simple structure) wasn't a win.

Perhaps if you were designing C++ from scratch without legacy and had a more Python-like mindset, you'd require an explicit "= default" rather than complicated rules about whether a default is provided for you or not.

Re:Modern C++ for C Programmers: part 5 by datamart on Wednesday July 25, 2018 @11:11AM (#17609863)
The implicitly-declared copy constructor is deleted if there is a user-defined move constructor. See: https://en.cppreference.com/w/cpp/language/copy_constructor#...

So if you define a move constructor and still want your class to have a copy constructor, you'll have to explicitly define the copy constructor, too.

Re:Modern C++ for C Programmers: part 5 by microtherion on Wednesday July 25, 2018 @12:28PM (#17610481)
Luckily, in modern C++, defining the copy constructor in this case (where you just want to make the default available) is as simple as:

    A::A(const A &other) = default;
Re:Modern C++ for C Programmers: part 5 by mikec3010 on Wednesday July 25, 2018 @11:14AM (#17609884)
That makes sense, and hence the "rule of 5" (if you don't want it implicitly deleted). Thanks.
Re:Modern C++ for C Programmers: part 5 by ahubert on Wednesday July 25, 2018 @11:09AM (#17609849)
(author here), it is true, try it. Sample code is on https://github.com/ahupowerdns/hello-cpp/blob/master/move.cc
Re:Modern C++ for C Programmers: part 5 by mikec3010 on Wednesday July 25, 2018 @11:12AM (#17609873)
Thanks, and sorry for not just trying it. I'm on mobile and our workstations here aren't cpp11 capable.