r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Aug 25 '21

In practise, I barely use iterators directly. Range based for loops and algorithms see to that. The only time I really use them is as the result of a find() != end(container) type situation, and that's hardly a gotcha situation.

0

u/pedersenk Aug 25 '21 edited Aug 25 '21

The range based loops do protect from most mistakes from wrangling iterators directly (this is good. I use them heavily at work!) but it is still very difficult to verify systematically that a function called somewhere further down the call stack will not invalidate the vector whilst going through it (vector<T> is mainly the issue but even list<T> can get invalidated in some rare cases). Also removing whilst iterating needs special care (often it is safer to avoid iterators completely for that part).

One I think is quite fun to demonstrate (and not all can agree on this within a couple of mins when I present it to them in interviews)

std::vector<uint32_t> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.emplace_back(v.at(1)); // Valid?
v.push_back(v.at(1)); // Valid?

Some discussion: here and here.

The standard has made a special case for allowing this (or it would be reading from invalidated memory just after the vector resizes). However when a vector is wrapped within layers of other peoples programs, they rarely provide the same special treatment. This kind of extra cognitive work thinking about it makes hobby personal projects with the standard C++ STL "un-fun".

Microsoft's safe iterators (unfortunately unavailable on platforms I use) obviously do some locking (as does my custom STL). In my opinion, more things should be locked (in the debug builds).

In my STL I do locking on '->' to alert me of dangling 'this'. I also lock (and bounds check) on array / vector[index]. Finally I also lock the lifespan of references received from string[idx] and vector[idx].

It results in a ~20% slowdown in debug builds. It can probably be optimized but that is fairly unnecessary. Release builds are generally all I care about for performance.

3

u/[deleted] Aug 25 '21

I dunno man, I feel like dangling pointers were a problem for me back in 2003, but they are just something that don't happen to me anymore, because of the way I naturally write C++. I don't do any funky stuff, and use RAII a lot.

3

u/pedersenk Aug 25 '21

Don't get me wrong, it very rarely happens (mainly since C++0x onwards). But the fact I can't guarantee it can never happen does ruin the fun a little for me. So that is why as a hobby I looked at resolving that.

Very likely OCD more than anything else ;)

1

u/[deleted] Aug 25 '21

Hehe fair enough :)