C++: Safe to use longjmp and setjmp?

前端 未结 2 756
情书的邮戳
情书的邮戳 2020-11-27 16:55

Is it safe to use longjmp and setjmp in C++ on linux/gcc with regards to the following?

  1. Exception handling (I\'m not implementing exception handling using long
2条回答
  •  感情败类
    2020-11-27 17:20

    setjmp()/longjmp() completely subvert stack unwinding and therefore exception handling as well as RAII (destructors in general).

    From 18.7/4 "Other runtime support" in the standard:

    If any automatic objects would be destroyed by a thrown exception transferring control to another (destination) point in the program, then a call to longjmp(jbuf, val) at the throw point that transfers control to the same (destination) point has undefined behavior.

    So the bottom line is that setjmp()/longjmp() do not play well in C++.

提交回复
热议问题