4 ms·
> where the caller of your method is a closing brace Although I agree with the sentiment of your comment destructors and RAII in general are arguably the best
by ExpiredLink 15y ago
> where the caller of your method is a closing brace
Although I agree with the sentiment of your comment destructors and RAII in general are arguably the best C++ features.
If I were forced to use C++ again I would restrict my code to a lightweight subset of C++, especially I would not use the C++ Standard library (STL, iostreams, string) and of course not use BOOST (or any template heavy library) and C++0x. But I would definitely include 'full-fledged' exception handling from the start.
- wladimir 15y agoWhat would you use for string handling, then? Please don't tell me you prefer strcpy/strcat and zero-terminated strings?
- cageface 15y agoYou could use bstring: http://bstring.sourceforge.net/ http://bstring.sourceforge.net/
- cygx 15y agoDepending on the use case, libc, Windows API or ICU. Personally, I'd like to see a simple C string library using modified UTF-8 (to be compatible with ASCIIZ) which is grapheme aware. I'm tempted to take a shot at writing one myself, but haven't gotten around to it yet. If anyone is interested, see http://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries http://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries for the algorithm and http://www.unicode.org/Public/UNIDATA/auxiliary/GraphemeBreakProperty.txt http://www.unicode.org/Public/UNIDATA/auxiliary/GraphemeBrea... for the property data...
- ExpiredLink 15y agoA class or template that doesn't need 3 template parameters.
- cageface 15y agoThe big problem with RAII and destructors is that you have no way of returning an error. You're not supposed to throw an exception and you have no way to return an error code so you can't really do much in a destructor if you want to write 100% correct code. Manually managing error codes is tedious but it's also more explicit and flexible.
- ExpiredLink 15y ago> The big problem with RAII and destructors is that you have no way of returning an error. That's actually a feature. Destructors are for cleanup. If code is supposed to report an error (by throwing an exception) it should not be put into a destructor.
- cageface 15y agoSo if I have an RAII object that supposed to close or delete a file and that fails for some reason, then what?
- ExpiredLink 15y agoIt depends. - If the file has been opened for reading only you can put fclose into the destructor and safely ignore the return value. - If the file has been opened for writing you need to call fclose() before the destructor to be able to react accordingly. Throwing an exception in the destructor would not be helpful in this case anyway because your scope is gone. BTW, writing a file safely is much harder than what many internet sources and textbooks make it appear.
- cageface 15y agoBut similar constructions in other languages (Python's with- statement, various Lisp versions, using in C#) can handle this cleanly. C++ doesn't give you "finally" and since you can't do non-trivial cleanup in destructors RAII is really a lot less useful than it seems at first blush.
- ExpiredLink 15y agoIn C++ you can write the cleanup code once in the destructor, "similar constructions in other languages" must be written every time you use it. You can and should do "non-trivial cleanup in destructors", e.g. DB rollback, but you should do only cleanup in destructors.