Hacker News new | ask | show | jobs
by ar15saveslives 3360 days ago
Define "bastardizing C". Which improvement do you consider as "failed attempts"? RAII, constructors/destructors, so you don't need to write spaghetti code to free resources reliably? Templates, so you can write sort() function once, for all comparable types? Lambdas? Or maybe GLib is somehow better than stl/boost?

C++ saves TONS of time and effort in our projects, thank god that I don't need to write in plain C anymore.

1 comments

> Templates, so you can write sort() function once,

C has a generic sort: qsort [1] (or mergesort, heapsort or radixsort if you have specific requirements).

[1] https://linux.die.net/man/3/qsort

Good luck chasing bugs then, though. qsort() will be totally happy comparing apples to oranges - aka there is zero type safety.

Templates give you compile time type checking, that's why one doesn't pass void pointers like this anymore but uses templates to implement generic functions.

> Good luck chasing bugs then, though. qsort() will be totally happy comparing apples to oranges - aka there is zero type safety.

That's a fair point. However, I do not tend to make many of the mistakes that would be caught by the C++ type system. But different people tend to make different kinds of mistakes.

The type bugs that happen tend to be pretty obvious and easy to debug. Good luck to you debugging template code :) I've had a harder time debugging C++ code than C code. Again, YMMV.

I agree that YMMV.

Although sometimes "I do not tend to make many of the mistakes that would be caught by the C++ type system" might be related to "how to use C++ type system so it would catch mistakes people tend to make".

qsort is both unsafer and slower than a good templated sort.
There are indeed trade-offs. But the comment I was replying to implied that you couldn't write a generic sort for all comparable types. That is wrong.

Unsafer yes.

But it's only slower because it's not being inlined. If the compiler can see both the implementation of qsort and the comparison function, there is no reason why it would be slower than a templated sort. You could accomplish this with link-time optimisation or by moving the implementation of qsort to a header file.

A trade-off you didn't mention: templates will cause multiple copies of the sort to be inlined in the executable. This can lead to bigger executables (with slower start-up) and more cache misses.

Another trade-off: templates lead to much slower compile times.

qsort might be general in the element type, but not in the container type. That is, it will only work for arrays. Also without LTO it will be very expensive as it cannot inline the compare function call. C++ sort is just faster and more powerful.
> qsort might be general in the element type, but not in the container type.

That is a clear advantage of C++.

> Also without LTO it will be very expensive as it cannot inline the compare function call.

Templates force you to expose the implementation in a header file. In C you can choose. If you move the qsort implementation in a header file, the compiler should not have any trouble inlining it.

Inlining is not always a win, though (slower compiles, bigger code, more cache misses).

Modules are supposed to fix this issue in some upcoming version of C++ standard.
That has been the case for the last 10 years. A module system for C++ is long overdue.