int compare(const void *a, const void *b)
{
return *((int*)a) < *((int*)b);
}
Yuck. int compare(const void *a, const void *b)
{
return *((int*)a) < *((int*)b);
}
Yuck. std::sort(begin(array), end(array), [](int a, int b) { return a < b; });
Versus: qsort(array, MAX_VAL, sizeof(int), compare);
With compare as defined before. I mean, sure, it's just sorting and it's a tiny example, but to me it shows a lot about why I prefer C++ over C: typesafety, readability, reduced possibility of buffer overflows, reduced possibility of segfaults based on incorrect element access, speed thanks to code inlining vs function pointers...Lest you think I'm being cavalier about this, I've been writing professional Ruby since ~2006, and have since then been routinely aggravated by bugs that would have been mitigated by type safety. I buy type safety. But it's a continuum of value, not a core principle of development.
In idiomatic C code, void* is a way of dropping in and out of static typing, usually to pass values of arbitrary types to a generic container library or through a callback. The use of void* doesn't surrender type safety throughout the program. Idiomatic C code casts a specific type to void* at the call site of the function that handles generic types, and casts it back to that specific type the moment that library hands it back.
Of the arguments C++ devotees marshall against C, I find this one among the fudliest.
Edit: By this I mean; if you want to prefer the C++ libraries, that's fine, but please don't claim C is inherently ugly.