Std :: current_exception related data lifetime

Consider the following code:

std::exception_ptr eptr{ std::current_exception() };
const char* msg = 0;
try {
    if (eptr != std::exception_ptr{}) {
        std::rethrow_exception(eptr);
    }
} catch(const std::exception& ex) {
    msg = ex.what();
}

Can I use msgout catch? In other words, exrefers to the same instance of an exception like eptr? Thanks!

+4
source share
2 answers

TL DR : I wouldn’t do this because exand eptr(maybe?) They refer to various exceptions. (see edit: it can be guaranteed, but the standard is not entirely clear so I cannot rely on it)

The lifetime of exceptions is determined by §15.1 [except.throw] , namely:

4/ , , . 7.3.4.1. , . , , , std::exception_ptr (§18.8.5), , , . , , , . std::exception_ptr. ; . [: , , ; . §18.8.5 §30.6. -end note]

:

3/ (§8.5, §12.8) , . lvalue , (§15.3). [...]

, :

  • eptr , , , ,
  • ex , eptr
  • catch,
  • msg , , ... .

, , - std::exception_ptr, .

, , §18.8.5 [] :

7/[...] [: rethrow_exception ( ), . exception_ptr, , . -end note]

, :

[[noreturn]] void rethrow_exception(exception_ptr p);

9/: p .

10/Throws: , p.

Throws throw *p;, , throw; ( , ). , if, 7, , ...


: , ... ; , - -, .

+3

rethrow_exception :

Throws: , p.

, , . " ", current_exception , .

, , , , eptr , , , .

+6

Source: https://habr.com/ru/post/1526481/


All Articles