Exception safety and make_unique

前端 未结 3 1388
盖世英雄少女心
盖世英雄少女心 2020-12-01 15:08

Just to clarify, using make_unique only adds exception safety when you have multiple allocations in an expression, not just one, correct? For example

         


        
3条回答
  •  情话喂你
    2020-12-01 15:16

    As of C++17, the exception safety issue is fixed by a rewording of [expr.call]

    The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.

    Here indeterminately sequenced means that one is sequenced before another, but it is not specified which.

    f(unique_ptr(new T), function_that_can_throw());
    

    Can have only two possible order of execution

    1. new T unique_ptr::unique_ptr function_that_can_throw
    2. function_that_can_throw new T unique_ptr::unique_ptr

    Which means it is now exception safe.

提交回复
热议问题