c++ - can rethrow_exception really throw the same exception object, rather than a copy? -
while examining exception_ptr
does, c++11 standard says (18.8.5/7) that:
use of rethrow_exception on exception_ptr objects refer same exception object shall not introduce info race. [ note: if rethrow_exception rethrows same exception object (rather copy), concurrent access rethrown exception object may introduce info race...
i don't find case weird "note" applies, since described effect of rethrow_exception
"throws: exception object p refers" 15.1/3, describing general exception throwing process mandates "throwing exception copy-initializes temporary object, called exception object."
the weird note imply rethrow_exception skips copy-initialization. possible?
yes, looks deficiency in standard. rethrowing throw-expression i.e. throw;
without operand, 15.1p8 says:
a throw-expression no operand rethrows handled exception. exception reactivated existing exception object; no new exception object created. [...]
that is:
#include <exception> #include <cassert> int main() { std::exception *p = nullptr; seek { seek { throw std::exception(); } catch(std::exception &ex) { p = &ex; throw; } } catch(std::exception &ex) { assert(p == &ex); } }
if implementation of current_exception
copies handled exception object, there's no way tell whether rethrow_exception
copies or not, if refers exception object can check:
#include <exception> #include <iostream> int main() { std::exception_ptr p; seek { seek { throw std::exception(); } catch(...) { p = std::current_exception(); std::cout << (p == std::current_exception()) << ' '; std::rethrow_exception(p); } } catch(...) { std::cout << (p == std::current_exception()) << '\n'; } }
every implementation i've tried on prints 1 1
; 0 0
allowed if current_exception
copies; 0 1
impossible, while standard in current state appears require 1 0
. prepare 18.8.5p10 clarified language similar 15.1p8, either allowing or mandating rethrow_exception
not re-create exception object pointed exception_ptr
.
most throws: specifications in standard name type (throws: bad_alloc
) or utilize indefinite article (throws: exception of type ...); other exception specifications utilize definite article of future::get
, shared_future::get
, resolution should address well.
c++ c++11 language-lawyer
No comments:
Post a Comment