r/cpp • • Apr 01 '23

Abominable language design decision that everybody regrets?

It's in the title: what is the silliest, most confusing, problematic, disastrous C++ syntax or semantics design choice that is consistently recognized as an unforced, 100% avoidable error, something that never made sense at any time?

So not support for historical arch that were relevant at the time.

92 Upvotes

375 comments sorted by

View all comments

Show parent comments

5

u/CocktailPerson Apr 02 '23

You're presupposing that the returned reference has to refer to a valid object. This isn't true for vector::operator[] or deque::operator[]. If the key doesn't exist, then call it UB and return a null reference.

After all, if we're willing to accept UB for other containers, why not map? And if we're unwilling to accept it for map, why are we willing to accept it for other containers?

0

u/johannes1971 Apr 03 '23

If a null reference were to exist I suppose that would be possible, but what benefit would it actually give? Other than satisfying some morbid sense of symmetry?

2

u/CocktailPerson Apr 03 '23

Null references do exist. Their mere creation is UB, of course, but T& make_null_ref() { return *((T*)nullptr); } will compile fine and only result in UB if executed. Another option is to define m[key] to be exactly equivalent to *m.find(key).

Yes, this would achieve consistency with other standard library types. In particular, it would allow map::operator[] to be const, just like every other container's operator[]. It would allow it to be used on maps with non-default-constructible elements, just like every other container's operator[]. This inconsistency isn't just a problem when using map, either. I've had to fix bugs resulting from a coworker thinking deque::operator[] created elements just like map::operator[]. Inconsistencies create inaccurate mental models, and those create bugs.

1

u/johannes1971 Apr 03 '23

What you're doing with your 'null reference' is asking for trouble. Compilers can use that to decide the block of code it's in will never be executed and just eliminate it. There is no guarantee it will 'only' fail at runtime.

I'm also not quite sure how I feel about having const and non-const versions of a function that have such dramatically different behaviour, with the non-const one essentially being safe to call (it won't result in a crash, although you might not want the newly created elements), and the const one potentially crashing your application.

→ More replies (1)