Reusing data member storage via placement new during enclosing object's lifetime

后端 未结 2 1839
一生所求
一生所求 2021-01-03 10:46

This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comme

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 11:20

    If a is destroyed (whether by delete or by falling out of scope), then t.~T() is called, which is UB if t isn't actually a T (by not calling destruct).

    This doesn't apply if

    • the destructor of T is trivial, or
    • for delete U is derived from T, or
    • you're using a destroying delete

    After destruct is called you are not allowed to use t if T has const or reference members (until C++20).

    Apart from that there is no restriction on what you do with the class as written as far as I can see.

提交回复
热议问题