我保证指针std :: vector元素在向量移动后是有效的吗? [英] Am I guaranteed that pointers to std::vector elements are valid after the vector is moved?
问题描述
考虑这个例子:
std :: vector< int> v1 = {1,2,3};
const int * i =& v1 [1];
std :: vector< int> v2(std :: move(v1));
std :: cout<< * i < std :: endl;
即使在许多STL实现中,这可能会工作,我保证标准没有重新分配在移动 std :: vector
时执行,并且内部缓冲区支持 v2
与以前一样 v1
之一?我无法在互联网上或标准本身上找不到此信息。
这是 LWG开放问题2321 [着重于我的]
移动容器应(通常)需要保留迭代器
[...]
[by Stephan T. Lavavej]
23.2.1 [container.requirements.general] / 10说除非另有说明,否则没有swap()函数无效任何引用,指针,
或引用容器的元素的迭代器
交换[注意:end()迭代器不引用任何元素,因此
可能无效。 - end note]。 但是,移动构造函数和
移动赋值运算符不会给出类似的无效
保证。保证需要几个异常,所以我不
相信毯子语言/ 11除非另有规定
(显式地或通过其他
函数定义函数),调用容器成员函数或将
容器作为参数传递给库函数,不会使
迭代器无效,或更改该容器中的对象的值。
适用。
[2014-02-13 Issaquah]
STL以提供更新的文字。 移至开启。
$ b> [...]
没有一个容器的移动构造函数[...](
array
除外)使引用源容器的元素的任何引用,指针或迭代器无效。 [注意:end()
迭代器不会引用任何元素,因此可能会失效。 ]
所以,这是一个开放的问题,指针不能通过移动而失效)。但是,它不是正式接受(但?)为缺陷。据我所知,所有主要的实现并不使指针在移动构造时无效,并且它似乎是一个通常(隐式)提供的保证。
Considering this example:
std::vector<int> v1 = { 1, 2, 3 };
const int* i = &v1[1];
std::vector<int> v2(std::move(v1));
std::cout << *i << std::endl;
Even though in many STL implementations this will probably work, am I guaranteed by the standard that no reallocations are performed when a std::vector
is moved, and the internal buffer backing v2
is the same that used to be the one of v1
? I wasn't able to find this information neither on the Internet, nor on the standard itself.
This is LWG open issue 2321 [emphasis mine]
Moving containers should (usually) be required to preserve iterators
[...]
[by Stephan T. Lavavej]
23.2.1 [container.requirements.general]/10 says that unless otherwise specified, "no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped. [Note: The end() iterator does not refer to any element, so it may be invalidated. — end note]". However, move constructors and move assignment operators aren't given similar invalidation guarantees. The guarantees need several exceptions, so I do not believe that blanket language like /11 "Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container." is applicable.[2014-02-13 Issaquah]
General agreeement on intent, several wording nits and additional paragraphs to hit.
STL to provide updated wording. Move to Open.
Proposed resolution:
[...]
no move constructor [...] of a container (except for
array
) invalidates any references, pointers, or iterators referring to the elements of the source container. [Note: Theend()
iterator does not refer to any element, so it may be invalidated. — end note]
So, this is an open issue, with general agreement on its basic solution (pointer shall not be invalidated by moving). However, it isn't officially accepted (yet?) as a defect. As far as I know, all major implementations do not invalidate pointers when move-constructing, and it seems to be a generally (implicitly) provided guarantee.
这篇关于我保证指针std :: vector元素在向量移动后是有效的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!