7 ms·
I've always liked to keep in mind that C++ was developed as an extension to C ( http://en.wikipedia.org/wiki/Cfront http://en.wikipedia.org/wiki/Cfront ), so mo
by userbinator 12y ago
I've always liked to keep in mind that C++ was developed as an extension to C ( http://en.wikipedia.org/wiki/Cfront http://en.wikipedia.org/wiki/Cfront ), so most of the constructs that it has can be directly mapped to C --- I think the only exception is... exceptions, although there's probably a way to do it using setjmp/longjmp too.
Personally I prefer C because of all the added flexibility it allows, but I can definitely see the value of a lot of the features added by C++ - C programmers were using structures and function pointers before, now they can use classes. RAII - a nice benefit of the semantics of constructors and destructors - is a more implicit, easier way of doing the same thing manually by calling the appropriate functions. Templates make it easy to generate a lot of otherwise very similarly structured code. Operator overloading can replace function calls and make some expressions look clearer.
However, the biggest disadvantage of all these features is that they add complexity and cognitive overhead to understanding the code - abstractions are only truly useful to those who know what the abstractions are of. That's why I believe that anyone who wants to learn C++ should already have strong knowledge of C (including structures, pointers, and memory allocation), and preferably know some Asm; then the transition to using the features of C++ feel straightforward and natural, without the "I don't know how it works, it's all magic" feeling to them. It's for this same reason that I recommend reading through the code of the STL - a good one to start with is std::vector.
- mtdewcmu 12y ago>>I think the only exception is... exceptions, although there's probably a way to do it using setjmp/longjmp too. I'm pretty sure that C++ exceptions could be simulated that way. >>Templates make it easy to generate a lot of otherwise very similarly structured code Templates are a mixed bag. There are a lot of things that look like they should be possible with templates, but once you start trying to implement them, it suddenly becomes much more difficult. Most of the practical value of templates is in getting around data type limitations and in inlining, IMO. Luckily, C has simpler data types than C++, so there is less need to climb over barriers, but templates do provide some value over C in this respect. The value with respect to inlining may be decreasing as C compilers become more sophisticated, but I'm not certain. std::sort() is said to be faster than qsort(). I've wondered if there is any way to coax a C compiler into inlining the sort routine, callback and all, but I haven't tried it yet.
- SamReidHughes 12y ago> There are a lot of things that look like they should be possible with templates, but once you start trying to implement them, it suddenly becomes much more difficult. No it doesn't.
- srean 12y ago>I've wondered if there is any way to coax a C compiler into inlining the sort routine, callback and all, but I haven't tried it yet. And how is the compiler supposed to inline the callback in qsort() when all it has is a pointer that will be provided only at runtime ? With JIT yes that would be possible. This requires a runtime optimization solution, not a compile time. This is also the reason why templates come so handy, sans the error messages (although GCC has improved by leagues, now to the point I find GCC's error messages easier to debug STL code with than Clang's). The error message scenario would a be a lot better once they get concepts sorted out.
- mtdewcmu 12y agoIt's probably impossible with qsort. I was wondering, if both the sort routine and the callback were declared static, would gcc be clever enough to inline both, eliminating the function pointer? It seems like it should be possible in principle.
- SamReidHughes 12y agoInline qsort, then inline the callback.
- Someone 12y agoI don't have stats, but I would think that the comparison function's definition typically is available at compile time. Also, if the compiler gets the qsort prototype by including it from the system header <stdlib.h>, I think the compiler may assume that it is the standard qsort function. So, it could emit a custom qsort function that inlines the comparison function.
- mtdewcmu 12y ago