3 ms·
>>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
by 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>I don't have stats, but I would think that the comparison function's definition typically is available at compile time. The comparison function usually will be. The qsort function generally won't be available. >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. It could do something like that, but gcc doesn't (https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Other-Builtins.html#Other-Builtins https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Other-Builtins....). If you look at the kinds of API functions implemented as builtins, they are generally simple.
- mitchty 12y ago> I'm pretty sure that C++ exceptions could be simulated that way. Indeed they can, may I introduce you to the preprocessor/macro extravaganza that is libextc: https://github.com/jspahrsummers/libextc/blob/master/src/exception.c https://github.com/jspahrsummers/libextc/blob/master/src/exc... I used it for a bit mostly to learn the chicanery of the madness. But I'm not a huge exception fan in general it just doesn't fit C well in my opinion.
- mtdewcmu 12y agoI've never seen much value in exceptions. For that matter, I've never had much interest in using longjmp and setjmp. It's possible that I tried them out at one point just to see them work. But I've never come up with a use for them.
- mitchty 12y agoWell if you try using setjmp/longjmp and then introduce threads with this stuff, I think you'll quickly realize where this whole rigamarole breaks down. I gave it a quick try but honestly its simpler to just use straightforward C.
- mtdewcmu 12y agoI admire your persistence and tenacity in getting as far as you did with that rigamarole. I'll take your word for it that it that it ends badly. :)
- mitchty 12y agoHeh thanks, it was more along the lines of: well i've never tried to get set/longjmp to work with threads, how hard can it be? Short answer, harder than you'd initially think. So many edge cases, the horror.
- mtdewcmu 12y agoIt seems like someone must have done it, if doing it is at all worthwhile...