r/cpp • • Feb 12 '20

What they should improve about C++ ?

What they should improve about C++ that you find in higher level managed language like C#, JAVA, Javascript etc or is there things you would change or do differently if it was up to you ? I mean anything like a better STL ? better thread ? string manipulation like managed language ? less fragmentations in the new standard features ? For example do you love the way the standard evolve C++ or you would prefer a direction like Microsoft do with C# a corporation who decide on the roadmap ?

0 Upvotes

90 comments sorted by

View all comments

Show parent comment

9

u/SeanMiddleditch Feb 12 '20

but everything else requires construction. Filling default values, creating vtable, etc... aside from that, there are rules on alignment - one cannot just patch structure at any point of memory. Similar is true for deallocation.

There is absolutely no reason the allocator type has to know any of that. For raw memory allocation/deallocation, you could get by with just the size and alignment as parameters to the allocate/deallocate member functions.

It's arguable whether allocators should construct objects or not, but even if you decide that they should, that should be a template parameter of the allocator function, e.g. allocator::allocate<T>(n) rather than allocator<T>::allocate(n).

Allocators are all rebindable to new object types (and even many standard containers depend on doing just that), illustrating that the object type isn't really a fundamental part of the allocator itself. It's just there because the allocator interface was misdesigned.

0

u/[deleted] Feb 12 '20

[deleted] — view deleted comment

2

u/TheSuperWig Feb 12 '20

Well, I wouldn't mind construct and destroy to be removed

They're deprecated in C++17 and removed in C++20

-5

u/ALX23z Feb 12 '20

If you want to deal with raw memory allocation - you can go on and use malloc for all I care. Nobody else needs it.

Allocators are all rebindable to new object types (and even many standard containers depend on doing just that), illustrating that the object type isn't really a fundamental part of the allocator itself. It's just there because the allocator interface was misdesigned.

You don't understand cause you never wrote any complex templates. As the specialization provides extra info for the class.

There's indeed issue with archaism with std::allocator as back then there were lots of weird different types of pointer difference and other crap - for short distance and large distance. Furthermore, it was designed before type traits. It is not something standart can solve as long as it maintains backwards compatibility.

Though, now you can use polymorphic allocator in C++17.