简体   繁体   English

具有可访问构造函数的std :: vector <T> :: iterator

[英]std::vector< T >::iterator with accessible constructor

g++ implementation of STL allows me in my client code to construct an iterator of std::vector<T>::iterator type with arbitrary address value: STL的 g ++实现允许我在客户端代码中构造具有任意地址值的std::vector<T>::iterator类型的std::vector<T>::iterator

int* i_ptr;
std::vector< int >::iterator it(i_ptr);

Is it standard ability, or just rudiment of implementation? 是标准能力还是仅仅是实施的雏形?

Where it designed to be used? 它在哪里设计使用?

This is just an implementation detail, it is not even guaranteed to compile by the standard. 这只是一个实现细节,甚至不能保证按标准进行编译。 It is also not listed in gcc's implementation defined behavior nor in the GNU C++ extensions, so your snippet may break at any version change. 它也未在gcc的实现定义的行为或GNU C ++扩展中列出,因此您的代码段可能会在任何版本更改时中断。

Don't rely on this "working" in any sense in your code. 不要在您的代码中以任何方式依赖此“工作”。

In Visual Studio 2017 RC this does not compile: 在Visual Studio 2017 RC中无法编译:

main.cpp(8): error C2664: 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>::
_Vector_iterator(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>> &&)':
cannot convert argument 1 from 'int *' to
'const std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>> &'

I do not think that it is a standard ability. 我认为这不是标准能力。 Just an implementation defined feature. 只是实现定义的功能。

In many implementations vector iterators are just simple pointers. 在许多实现中, 矢量迭代器只是简单的指针。 So if you have a vector< T > then iterator is simply a pointer to type T . 因此,如果您有一个vector <T>,则迭代器只是一个指向T的指针。 It is done for some optimization purposes. 出于某些优化目的而完成。

In such implementations it is possible to create iterators initialized from the pointers to type T because you can initialize a pointer to type T by another pointer of such type. 在此类实现中,可以创建从指针初始化为类型T的迭代器,因为您可以通过此类的另一个指针将指针初始化为类型T。

But it is implementation defined, of course, if an iterator defined as a pointer to type T or not. 但这当然是实现定义的,无论是否将迭代器定义为指向T类型的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM