←back to thread

178 points todsacerdoti | 4 comments | | HN request time: 1.405s | source
Show context
codeflo ◴[] No.26340405[source]
I’ve personally never encountered the particular misunderstanding the author dispels here, but I’m sure the post is written from bitter experience. On the one hand, I shouldn’t be that surprised: Unfortunately, programming in C++ is really brutal for people who would rather learn by example than by carefully reading documentation.

But on the other hand, my instinctive reaction is: Come on, is this really something that confuses people? emplace_back is all about constructing something in-place, which is exactly the opposite of moving it there! How can any professional programmer be 180 degrees wrong about the purpose of a function they use regularly?

What I want to say here is that C++ is really hard, and that there are a million subtle things that one simply has to look up instead of making educated guesses. I don’t think people appreciate this difficulty enough.

replies(6): >>26340522 #>>26340785 #>>26344462 #>>26345318 #>>26345544 #>>26346091 #
mettamage ◴[] No.26340522[source]
I learn C++ by example by using the VSCode debugger. I prefer it over gdb with tui. It won’t go all the way (e.g. educational/smallish projects only that are single threaded) but by that time a programmer has seen some wonky things happening that they can hypothesize about. In my case:

- dangling pointers

- integer under/overflow

- macro expansion (not via debugger but VSCode helps with this)

- double values not being the same when you expect them to be the same

- Seeing if something is copied or moved [1]

[1] Not sure if this is actually true. But I think it can be true by simply stepping inside the function like emplace_back

replies(1): >>26340935 #
vbezhenar ◴[] No.26340935[source]
If you're using Linux, run your test programs under valgrind. It'll help you immensely to find memory issues.
replies(1): >>26341317 #
1. readams ◴[] No.26341317[source]
I mostly use address sanitizer these days rather than valgrind. It's fast enough that you can just put it in your default compile flags for debugging.
replies(3): >>26341939 #>>26342018 #>>26345905 #
2. scatters ◴[] No.26341939[source]
And it's available for Visual Studio, so you don't even need to be running Linux.
3. junon ◴[] No.26342018[source]
Address sanitizers have bitten me before. Valgrind has not, aside from the times it wasn't supported on new macos.

Just be careful.

4. usefulcat ◴[] No.26345905[source]
It is faster, but IME valgrind is more thorough. I have an option to run unit tests with valgrind.