Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,950 of 33,346    |
|    =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All    |
|    Re: Forcing non-throwing arguments    |
|    22 Feb 12 08:06:37    |
      87fa85df       From: daniel.kruegler@googlemail.com              Am 18.02.2012 22:55, schrieb Dmitry Potapov:       > Disclaimer: I suspect the trick described below is obvious, but I failed       > to find any posts in this group which is referring to it, so I just       > leave it here.       >       > There was a big problem in C++03, when accepting template argument, you       > know nothing about exceptions which can occur while you're using this       > argument. Sometimes, this can lead to additional logic and performance       > penalty.       >       > As for me, I don't care about exceptions type, it is the caller who       > should handle them. My job is to prevent resource leaks and I need to       > know if execution sequence can be interrupted by exception.       >       > In C++11 there is noexcept operator, which allows to determine if some       > expression can throw an exception. From this, it is become possible to       > provide two function implementations, one for exception throwing       > argument, and second one with noexcept exception specification.       >       > For example, consider class C with template c'tor which allocates some       > resources and then calls member function f() of the argument. It is a       > good practice to wrap resources with unique_ptr or something similar in       > order to avoid resource leak, but this introduces slight overhead for       > non-throwing arguments, which can be unacceptable in       > performance-critical applications. So, public c'tor accepting argument       > can delegate its work to one of the overloaded c'tors which accepts       > std::true_type and std::false_type accordingly:              I agree that this is a nice idiom. A very minimalistic form of this kind       of exception-based flow control is part of the standard library:       move_if_noexcept (In this case it does not perform different actions,       but it has been added to take advantage of such "exception branching").              > #include |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca