std::vector、std::move 和指针失效
std::vector, std::move and pointer invalidation
我的问题与以下模式有关...
我想用下面的模式构造一个昂贵的构造SomeData
,然后将它移动到UsesData
.
所以问题是...
ud.dat.m_ptrs
中的指针是否保证仍然有效?
struct BigObject{};
struct SomeData
{
SomeData() = default;
SomeData(const SomeData &) = delete;
SomeData & operator = (const SomeData &) = delete;
SomeData(SomeData &&) = default;
SomeData & operator = (SomeData &&) = default;
std::vector<BigObject> m_data1; // big vector
std::vector<BigObject> m_data2; // big vector
// (m_ptrs.size() == m_data1.size() + m_data2.size())
// points to elements in m_data1 and m_data2...
std::vector<const BigObject * const> m_ptrs;
};
struct Builder
{
Builder() = delete;
Builder(const Builder &) = delete;
Builder & operator=(const Builder &) = delete;
Builder(Builder &&) = delete;
Builder & operator=(Builder &&) = delete;
Builder(int a)
{
// makes sure BigObject vectors in SomeDate are constructed correctly
// builds m_ptrs... vector of ptrs to m_data1 and m_data2
}
SomeData dat;
};
struct UsesData
{
UsesData() = delete;
UsesData(const UsesData &) = delete;
UsesData & operator=(const UsesData &) = delete;
UsesData(UsesData &&) = delete;
UsesData & operator=(UsesData &&) = delete;
UsesData(Builder && from) : dat{ std::move(from.dat) }
{}
const SomeData dat;
};
int main()
{
UsesData ud{ Builder{ 1 } };
//...
}
指针将保持有效。根据std::vector
的move constructor的行为:
After container move construction (overload (6)), references,
pointers, and iterators (other than the end iterator) to other remain
valid, but refer to elements that are now in *this
. The current
standard makes this guarantee via the blanket statement in
§23.2.1[container.requirements.general]/12, and a more direct
guarantee is under consideration via LWG 2321.
这意味着,在被移动之后,指针仍然有效并指向被移动到新std::vector
的元素。
我的问题与以下模式有关...
我想用下面的模式构造一个昂贵的构造SomeData
,然后将它移动到UsesData
.
所以问题是...
ud.dat.m_ptrs
中的指针是否保证仍然有效?
struct BigObject{};
struct SomeData
{
SomeData() = default;
SomeData(const SomeData &) = delete;
SomeData & operator = (const SomeData &) = delete;
SomeData(SomeData &&) = default;
SomeData & operator = (SomeData &&) = default;
std::vector<BigObject> m_data1; // big vector
std::vector<BigObject> m_data2; // big vector
// (m_ptrs.size() == m_data1.size() + m_data2.size())
// points to elements in m_data1 and m_data2...
std::vector<const BigObject * const> m_ptrs;
};
struct Builder
{
Builder() = delete;
Builder(const Builder &) = delete;
Builder & operator=(const Builder &) = delete;
Builder(Builder &&) = delete;
Builder & operator=(Builder &&) = delete;
Builder(int a)
{
// makes sure BigObject vectors in SomeDate are constructed correctly
// builds m_ptrs... vector of ptrs to m_data1 and m_data2
}
SomeData dat;
};
struct UsesData
{
UsesData() = delete;
UsesData(const UsesData &) = delete;
UsesData & operator=(const UsesData &) = delete;
UsesData(UsesData &&) = delete;
UsesData & operator=(UsesData &&) = delete;
UsesData(Builder && from) : dat{ std::move(from.dat) }
{}
const SomeData dat;
};
int main()
{
UsesData ud{ Builder{ 1 } };
//...
}
指针将保持有效。根据std::vector
的move constructor的行为:
After container move construction (overload (6)), references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in
*this
. The current standard makes this guarantee via the blanket statement in §23.2.1[container.requirements.general]/12, and a more direct guarantee is under consideration via LWG 2321.
这意味着,在被移动之后,指针仍然有效并指向被移动到新std::vector
的元素。