Live data from Hacker News

Modern C++ for C Programmers: part 5

ds9a.nl

31–40 of 82 posts

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

#31

Earlier quoted context omitted.

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.

There could be some more fundamental differences due to the requirement that different objects require different pointers that prevent some optimizations.

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

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

Absolutely There keeps being proposals for C++ standard modules, and it keeps getting punted. Having a reasonable module system, and hell even some lovely tools for migrating sane projects over to it, would make me much happier with the language. For what it's worth, Clang has experimental support for C++ modules.

It is not getting punted, but is being worked on.

The trouble being actually indexing the contents of any given module and trouble with template instantiation.

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

#34

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)…

After dabbling a bit in C++ after college, then purposely avoiding it for about twenty years, I'm getting back into it for work. I'd say my favorite books for coming up to speed were C++ Common Knowledge, Effective C++ (3rd edition), and Effective Modern C++, more or less in that order. Discovering Modern C++, which I haven't finished yet, has not been as enjoyable.

I wouldn't get too hung up on skipping straight to "modern" C++. Yes, C++11 is a game changer, but the vast majority of what you'll find in the classic recommendations is still worth knowing. Things like type deduction, smart pointers, and move semantics won't be hard to pick up once you've got the basics. Although I haven't read it, Accelerated C++ sounds like a good fit for your situation.

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

#35
post #27
post #25

Earlier quoted context omitted.

What's the motivation behind this?

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 exp…

[deleted]

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

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

Includes mixed with #ifdef leaks state across module borders and the whole C preprocessor feels like a dirty hack.

What a header actually does depends on compiler flags, #defines from other files and then the order in which you include things as one header file might define something that changes the behavior of another header file. The compiler loses state in between C++-files and caching precompiled header files was still a joke the last time I've checked.

That leads to the problem that every single C++ file needs to include a few hundred thousand lines of nested headers and compilation takes forever. Add in some templates and you get a lot duplicated code in the object files that needs to be weeded out by the linker. Sometimes it's even suggested to put all your classes into one single big C++ file and only compile that in order to get your compilation time down.

Having proper modules would do so much good!

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

#37
post #6

I wish I had a project that warranted C++ right now. It’s a magnificent language.

Agreed, I actually really like C++ nowadays. I usually tinker with compilers in higher-level languages, but my current project I'm using LibJIT, and wanted to go all-native.

I've had a blast with OOP in C++, believe it or not, and writing my recursive descent parser, symbol table, and analyzer felt like working with a higher-level language.

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

#38

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)…

Yes! And be careful and very picky about what you read! To quote a popular Quora answer [1]:

"If you really want to learn C++, I advise against any and all online tutorials and against most books. Without any kind of quality control, many professional book writers published many awful C++ textbooks that may be easy to read and sell well, but teach lies. Stick with the community-verified book list available at StackOverflow"

In other words, use the StackOverflow list to choose the right teaching material [2]. A Tour of C++ (Bjarne Stroustrup) has been a good refresher, with C++ Primer (Stanley Lippman, Josée Lajoie, and Barbara E. Moo) as the primary reference for further clarification. Also use GeeksForGeeks and LeetCode practice problems.

[1] https://www.quora.com/Which-is-the-best-book-for-learning-c+... [2] https://stackoverflow.com/questions/388242/the-definitive-c-...

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

#39
post #30

Earlier quoted context omitted.

Performance critical.

Also if Rust is not cutting it yet in your specific niche.

I find D to be a good tradeoff between C++ and Rust. I can't stand not having compile-time evaluation, code generation and reflection in Rust and C++ is just too slow to iterate with.

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

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

Includes mixed with #ifdef leaks state across module borders and the whole C preprocessor feels like a dirty hack. What a header actually does depends on compiler flags, #defines from other files and then the order in which you include things as one header file might define something that changes the behavior of another header file. The compiler loses state in between C++-files and caching precompiled header files wa…

Compiling Chromium, the biggest C++ project I know, takes on the order of 8 hours with a somewhat old but quite powerful server. Linking Chromium on my laptop, with 8GB of RAM, required me to increase my swap space from 8GB to 16GB.

Going away from C's linker and header file-model, which really doesn't work well for C++, to something based on a proper module system, would be amazing.

Post reply on HN