Live data from Hacker News

Modern C++ for C Programmers: part 5

ds9a.nl

11–20 of 82 posts

Re: Modern C++ for C Programmers: part 5

#11

> 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.

(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

#12

> 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.

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

#13

is there a modern C++ for python programmers? i have ~5 years programming in dynamic/interpreted (python/js) and compiled/gc'd(java/go) languages and i'd like to learn a systems language. i'm reading the rust programming book (and it's really good/easy to grok) but i'd also like to learn C++. the problem is that most books are either too easy (C++ as your first language) or too hard (straight into RAII and templates)…

Have you tried "A Tour of C++"? https://www.safaribooksonline.com/library/view/a-tour-of/978...

Re: Modern C++ for C Programmers: part 5

#14
post #11

> 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.

(author here), it is true, try it. Sample code is on https://github.com/ahupowerdns/hello-cpp/blob/master/move.cc

Thanks, and sorry for not just trying it. I'm on mobile and our workstations here aren't cpp11 capable.

Re: Modern C++ for C Programmers: part 5

#15

> 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.

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.

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

#16
post #7

Please please please can we get rid of headers and have modules? I find it super annoying having to specify things half in one place and half in another. Unfortunately, there still seems to be a bit of disagreement on the implementation among the standards committee.

Certain IDE vendors could solve this if they really cared about UX. Not by implementing modules, but by efficiently scanning vast directories for versions of headers, and arranging them in a visually navigable manner (you know, actually being a useful IDE). The last time I used one, the OS was incapable of even searching folders. Partial string matching of filenames was apparently too complex for it to handle.

Re: Modern C++ for C Programmers: part 5

#17

> 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.

"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

#18
post #7

Please please please can we get rid of headers and have modules? I find it super annoying having to specify things half in one place and half in another. Unfortunately, there still seems to be a bit of disagreement on the implementation among the standards committee.

I actually don't mind header files, but I do mind includes as the mechanism for stitching things together. Also, C++ doesn't have any concept of a package. You get libraries and headers and it's up to you to feed them to get them to the compiler correctly.

Re: Modern C++ for C Programmers: part 5

#19
post #13

is there a modern C++ for python programmers? i have ~5 years programming in dynamic/interpreted (python/js) and compiled/gc'd(java/go) languages and i'd like to learn a systems language. i'm reading the rust programming book (and it's really good/easy to grok) but i'd also like to learn C++. the problem is that most books are either too easy (C++ as your first language) or too hard (straight into RAII and templates)…

Have you tried "A Tour of C++"? https://www.safaribooksonline.com/library/view/a-tour-of/978...

Ordered a paper version on Amazon UK one month ago. Does anyone get the book already?

Re: Modern C++ for C Programmers: part 5

#20

Return value optimization is absolutely allowed in C, and ~all compilers do it. It's not commonly talked about because in C, there can be no side-effects when constructing or copying a struct, so there's nothing to discuss. It just happens automatically and there's no observable change in program behavior.

No, C has much less RVO allowed than C++. I provided an example of this here the last time : https://news.ycombinator.com/item?id=17216250

From what I can see, the example you list there is just a missed optimization in the C compilers you tested. It's absolutely allowed, and clang trunk is happy to do it in C-mode, for example.
Post reply on HN