简体   繁体   English

reinterpret_cast指向指针的迭代器

[英]reinterpret_cast an iterator to a pointer

I've got an iterator of Things. 我有一个东西的迭代器。 If I want to convert the current item to a pointer to the item, why does this work: 如果我想将当前项转换为指向该项的指针,为什么这样做:

thing_pointer = &(*it);

But this not: 但这不是:

thing_pointer = reinterpret_cast<Thing*>(it);

This is the compiler error I'm trying to comprehend: http://msdn.microsoft.com/en-us/library/sy5tsf8z(v=vs.90).aspx 这是我想要理解的编译器错误: http//msdn.microsoft.com/en-us/library/sy5tsf8z(v = vs。90).aspx

Just in case, the type of the iterator is std::_Vector_iterator<std::_Vector_val<Thing,std::allocator<Thing> > > 以防万一,迭代器的类型是std::_Vector_iterator<std::_Vector_val<Thing,std::allocator<Thing> > >

In

&(*it);

the * is overloaded to do what you logically mean: convert the iterator type to its pointed-to object. *重载以执行逻辑上的意思:将迭代器类型转换为其指向的对象。 You can then safely take the address of this object. 然后,您可以安全地获取此对象的地址。

Whereas in 而在

reinterpret_cast<Thing*>(it);

you are telling the compiler to literally reinterpret the it object as a pointer. 您告诉编译器将it对象重新解释为指针。 But it might not be a pointer at all -- it might be a 50-byte struct, for all you know! 但它可能根本不是一个指针 - 它可能是一个50字节的结构,对于你所知道的一切! In that case, the first sizeof (Thing*) bytes of it will absolutely not happen to point at anything sensible. 在这种情况下,它的第一个sizeof (Thing*)字节绝对不会指向任何合理的东西。

Tip: reinterpret_cast<> is nearly always the wrong thing. 提示: reinterpret_cast<>几乎总是错误的。

Obligitory Standard Quotes, emphasis mine: 义务标准行情,强调我的:

5.2.19 Reinterpret cast 5.2.19重新解释演员表

1/ [...] Conversions that can be performed explicitly using reinterpret_cast are listed below. 1 / [...]下面列出了可以使用reinterpret_cast显式执行的转换。 No other conversion can be performed explicitly using reinterpret_cast. 使用reinterpret_cast不能显式执行其他转换。

4/ A pointer can be explicitly converted to any integral type large enough to hold it. 4 /指针可以显式转换为足以容纳它的任何整数类型。 [...] [...]

5/ A value of integral type or enumeration type can be explicitly converted to a pointer. 5 /整数类型或枚举类型的值可以显式转换为指针。 [...] [...]

6/ A function pointer can be explicitly converted to a function pointer of a different type. 6 /函数指针可以显式转换为不同类型的函数指针。 [...] [...]

7/ An object pointer can be explicitly converted to an object pointer of a different type. 7 /对象指针可以显式转换为不同类型的对象指针。 [...] [...]

8/ Converting a function pointer to an object pointer type or vice versa is conditionally-supported. 8 /有条件地支持将函数指针转换为对象指针类型(反之亦然)。 [...] [...]

9/ The null pointer value (4.10) is converted to the null pointer value of the destination type. 9 /空指针值(4.10)被转换为目标类型的空指针值。 [...] [...]

10/ [...] “pointer to member of X of type T1” can be explicitly converted to [...] “pointer to member of Y of type T2” [...] 10 / [...]“指向T1类型X成员的指针”可以显式转换为“指向T2类型Y成员的指针”[...]

11/ A [...] T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. 如果可以使用reinterpret_cast将“指向T1的指针”类型的表达式显式转换为“指向T2的指针”类型,则可以将11 / A [...] T1转换为“对T2的引用”类型。 [...] [...]

With the exception of the integral-to-pointer and value-to-reference conversions noted in 4/, 5/ and 11/ the only conversions that can be performed using reinterpret_cast are pointer-to-pointer conversions. 除了4 /,5 /和11 /中提到的积分到指针和值到参考的转换之外,可以使用reinterpret_cast执行的唯一转换是指针到指针的转换。

However in: 但是在:

thing_pointer = reinterpret_cast<Thing*>(it);

it is not a pointer , but an object. it不是指针 ,而是对象。 It just so happens that this object was designed to emulate a pointer in many ways, but it's still not a pointer. 事实上,这个对象被设计为以多种方式模拟指针,但它仍然不是指针。

  1. Because * operator of iterator is overloaded and it return a reference to the object it points on. 因为迭代器的*运算符被重载并且它返回对它所指向的对象的引用。
  2. You can force it by thing_pointer = *(reinterpret_cast<Thing**>(&it)); 你可以通过thing_pointer = *(reinterpret_cast<Thing**>(&it));强制它thing_pointer = *(reinterpret_cast<Thing**>(&it)); . But it's undefined behavior. 但这是未定义的行为。

Because iterator is not a pointer. 因为迭代器不是指针。 It is a class of implementation-defined structure, and if you try to reinterpret it to a pointer, the raw data of the iterator class will be taken as a memory pointer, which may, but probably will not point to valid memory 它是一类实现定义的结构,如果你试图将它重新解释为一个指针,迭代器类的原始数据将被视为一个内存指针,它可能,但可能不会指向有效的内存

The first gets a reference to the object, then takes the address of it, giving the pointer. 第一个获取对象的引用,然后获取它的地址,给出指针。

The second tries to cast the iterator to a pointer, which is likely to fail because most types can't be cast to pointers - only other pointers, integers, and class types with a conversion operator. 第二个尝试将迭代器强制转换为指针,这可能会失败,因为大多数类型都不能转换为指针 - 只有其他指针,整数和具有转换运算符的类类型。

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

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