Can placement-new and vector::data() be used to replace elements in a vector?

后端 未结 2 872
时光取名叫无心
时光取名叫无心 2020-12-30 02:33

There are two existing questions about replacing vector elements that are not assignable:

  • C++ Use Unassignable Objects in Vector
  • How to push_back with
2条回答
  •  既然无缘
    2020-12-30 03:15

    This is illegal, because 3.8p7, which describes using a destructor call and placement new to recreate an object in place, specifies restrictions on the types of data members:

    3.8 Object lifetime [basic.life]

    7 - If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object [...] can be used to manipulate the new object, if: [...]
    — the type of the original object [...] does not contain any non-static data member whose type is const-qualified or a reference type [...]

    So since your object contains a const data member, after the destructor call and placement new the vector's internal data pointer becomes invalid when used to refer to the first element; I think any sensible reading would conclude that the same applies to other elements as well.

    The justification for this is that the optimiser is entitled to assume that const and reference data members are not respectively modified or reseated:

    struct A { const int i; int &j; };
    int foo() {
        int x = 5;
        std::vector v{{4, x}};
        bar(v);                      // opaque
        return v[0].i + v[0].j;      // optimised to `return 9;`
    }
    

提交回复
热议问题