News for hackers. Stuff that matters.
Re:Mapping Strings in C++ by _pdp_ on Wednesday April 12, 2017 @04:27PM (#14101416)
I wrote a 3D engine in C++ many years ago and it still feels wrong. At least with C you get the power to do things the way you want to and yes it is mostly unsafe if you don't know what you are doing. IMHO C++ is a failed attempt to reinvent already working language. C should not be extended. There are other languages like go and rust that provide good performance while not bastardizing C.
Re:Mapping Strings in C++ by bstamour on Wednesday April 12, 2017 @05:08PM (#14101770)
> At least with C you get the power to do things the way you want to

C++ gives you the same level of control over your program that C does, but also comes with nice low-cost (in many cases zero-cost) abstractions for when you need them.

Re:Mapping Strings in C++ by ar15saveslives on Wednesday April 12, 2017 @04:42PM (#14101550)
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.

Re:Mapping Strings in C++ by pg314 on Wednesday April 12, 2017 @06:33PM (#14102358)
> 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

Re:Mapping Strings in C++ by janoc on Wednesday April 12, 2017 @06:45PM (#14102438)
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.

Re:Mapping Strings in C++ by pg314 on Thursday April 13, 2017 @05:33AM (#14105419)
> 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.

Re:Mapping Strings in C++ by koja86 on Thursday April 13, 2017 @12:30PM (#14108474)
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".

Re:Mapping Strings in C++ by emmelaich on Wednesday April 12, 2017 @06:44PM (#14102429)
qsort is both unsafer and slower than a good templated sort.
Re:Mapping Strings in C++ by pg314 on Thursday April 13, 2017 @05:23AM (#14105391)
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.

Re:Mapping Strings in C++ by lederhosen on Wednesday April 12, 2017 @07:01PM (#14102558)
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.
Re:Mapping Strings in C++ by pg314 on Thursday April 13, 2017 @06:15AM (#14105578)
> 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).

Re:Mapping Strings in C++ by AstralStorm on Thursday April 13, 2017 @07:32AM (#14105913)
Modules are supposed to fix this issue in some upcoming version of C++ standard.
Re:Mapping Strings in C++ by pg314 on Thursday April 13, 2017 @02:07PM (#14109445)
That has been the case for the last 10 years. A module system for C++ is long overdue.
Re:Mapping Strings in C++ by khedoros1 on Wednesday April 12, 2017 @04:34PM (#14101485)
C++ today and C++ many years ago are very different languages.
Re:Mapping Strings in C++ by jnordwick on Wednesday April 12, 2017 @05:04PM (#14101731)
While I dislike c++as much as the next guy, you've pretty far off topic.
Re:Mapping Strings in C++ by fsloth on Thursday April 13, 2017 @04:57AM (#14105286)
"I wrote a 3D engine in C++ many years ago and it still feels wrong"

Only half of the reasons why C++ is so popular have to do with the language itself. The other half is related to constraints, that don't have as robust solutions using other languages.

Nobody is claiming C++ is a great language but for a lot of constraints there are no viable options.

Re:Mapping Strings in C++ by adrianN on Thursday April 13, 2017 @06:31AM (#14105657)
You can write C++ that looks almost identical to the C you would write. If you don't like fancy features you don't have to use them.
Re:Mapping Strings in C++ by blt on Wednesday April 12, 2017 @05:33PM (#14101972)
how did you like doing all the vector math in C?