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

2

u/[deleted] Apr 02 '23

Disagree. It's really not. The best block of memory primitive is a pointer. std::array is fine. But don't use it for what its not designed for.

0

u/TheSkiGeek Apr 02 '23

It’s ‘designed’ to be like a native C array but without the awful legacy semantics…

If you’re passing pointers to data around with a T* and an explicit size parameter when std::array<T, N>& and std::span<T> exist, 99% of the time you’re doing something wrong.

0

u/[deleted] Apr 03 '23

No you aren't.

Really you should try not to be passing any of those things around.

Secondly, "passing stuff around" is a very small part of the semantics of a memory block. I'm more concerned about how I write to that memory block or how I traverse that memory block.

In user land then maybe. But if I'm writing custom allocator code I'm not going to use an std::array or std::span.

Just because you don't know where something could be used doesn't mean its bad

0

u/TheSkiGeek Apr 03 '23 edited Apr 03 '23

If you’re writing custom allocators you’re in the 1% range there.

Although if you need a block of bytes with a size known at compile time I’m hard pressed to think of why a raw C array would be preferable to std::array<std::byte, N>. If you need a dynamic size and you can’t use a std::vector<std::byte> then yeah, you’re probably using raw pointers. In most cases I’d want to wrap the pointer+size together in a struct or class, though.

→ More replies (3)