5 ms·
Oh yeah, one last thing: not an example, but a pretty useful reference- Google's C++ Style Guide. https://google.github.io/styleguide/cppguide.html https://goo
by jchw 9y ago
Oh yeah, one last thing: not an example, but a pretty useful reference- Google's C++ Style Guide.
https://google.github.io/styleguide/cppguide.html https://google.github.io/styleguide/cppguide.html
I've not read the whole thing, but it looks pretty solid. Of course, there's a lot of highly subjective suggestions, but I can get behind many of them personally.
- gumby 9y agoNote that the style guide contains recommendations specific to Google's situation (they note this in the guide). For example they recommend against exceptions because by the time the style guide was written they already had an installed base. Had they started over they would use them. Having recently started a project from "empty buffer" state we elected to go with C++17 (supported now in both GCC and clang although with some library features still missing). It's worked out well for us, but in my case I approached it as a brand new language rather than an evolution of old C++ (which I'd started in the cfront days and dropped around 2000). In that light it became and expressive and powerful systems programming language that is fun to use.
- jchw 9y agoTo be honest, I still don't use exceptions. They don't really feel all that great in C++, and I think Go has solidified my anti-exceptions attitude. A lot of the STL and standard libraries and even many third party libraries also abstain from using exceptions since they behave poorly across library boundaries on many platforms thanks to the lack of properly standardized ABI, and of course because the standard libraries try to be as unassuming as possible. Of course, you can still more or less safely use exceptions if you control your ecosystem, but since this is the case, it gives a (pardon my french) PHP feeling of inconsistency in many ways. There may be compelling reasons to use exceptions, but if there were I think they have to be quite compelling to overcome the limitations.
- Ace17 9y ago> since they behave poorly across library boundaries on many platforms thanks to the lack of properly standardized ABI, The same also holds for, say, std::string. You can't pass them across library boundaries without a unified ABI. More generally, you can't pass anything STL-ish between library boundaries. Why make a special case for C++ exceptions?
- jchw 9y agoBecause throwing an exception can bubble across library boundaries, including over C-style APIs that are fully compatible across library boundaries.
- mhh__ 9y agoIf your C-api throws and then doesn't handle exceptions, then it's not a C api, it just happens to use the C ABI.
- jchw 9y agoIt's more complicated than that, though, because a C library seldom controls the callstack. Exception throwing is viral. A valid example would be using a pure, fully ANSI C library with no dependencies in a C++ program, but then, in a callback PASSED to C APIs, accidentally allowing an exception to leak. Then, your runtime will try to unwind the stack and can implode on itself. There's definitely more valid cases where this can happen, but since C APIs generally do rely on callbacks for many things, it seems like a logical one. Another case would be C-style dispatch tables like SDLs RW interfaces. Implementing them to interact with C++ classes could definitely cause problems. Of course, happening to use the C ABI is a valid use case that actually works if you really only use the C ABIs, but once a single exception is raised all bets are off. This kind of sucks.
- mhh__ 9y agoI said not handling the exception, not that one shouldn't use exceptions. Handling, here, could also mean using callbacks so the C code (using the api) can customize things. If you expose a C api, using C++ code that involves exceptions, then the exceptions should all be handled (caught) inside the C++ code. This means that should the runtime start unwinding, it will not terminate the program.
- pjmlp 9y agoThis is one of the reasons I never liked to work with certain C++ teams back on my C++ days and drifted to other languages that don't allow to turn features off, because I always enjoyed having exceptions and RTTI turned on.
- jchw 9y agoReally? I've used RTTI a handful of times but to be honest with you aside from debugging I very rarely had a use for them. I think C++ could benefit from more powerful reflection, though. Do you have any simple examples of exploiting RTTI to solve problems?
- pjmlp 9y agoYep dynamic_cast is not guaranteed to compile with RTTI disabled. Doing static_cast instead, will open the door to fun surprises if one cannot guaranttee the safety of the pointers given to the library code. I must add that in those days, we were making use of C++ gui libraries like OWL, followed by VCL and MFC.