Validity of pointers to internal data structure when reallocating a std::vector object

后端 未结 3 1592
北荒
北荒 2021-02-19 08:25

Assume there is a class A that contains a vector of ints. Now assume that a vector of A\'s is created. If a reallocation of an A object occurs (so the vector object is moved) du

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 08:52

    It would depend of your move constructor of A. but as it (*), it will use move constructor of vector for a, and according to http://www.cplusplus.com/reference/vector/vector/vector/

    [..] no elements are constructed (their ownership is directly transferred).

    So pointers to the ints themselves remain valid.

    Edit: (*) A should be noexcept for that, and std::vector is not guaranteed to be noexcept.

提交回复
热议问题