static_cast的指针值 [英] static_cast'd pointer value

查看:319
本文介绍了static_cast的指针值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在当前的标准草案(和C ++ 17)中,是关于static_casting void *:

In the current draft standard (and C++17), this is written about static_casting a void *:

可以将指向 cv1 的指针"类型的prvalue转换为指向 cv2 的指针" T的prvalue,其中T是对象类型,而 cv2 具有与 cv1 相同的cv资格或更高的cv资格.如果原始指针值表示内存中字节的地址A,而A不满足T的对齐要求,则未指定结果指针值.否则,如果原始指针值指向对象 a ,并且存在类型为T(忽略cv限定)的对象 b ,该对象可与 a ,结果是指向 b 的指针.否则,转换后指针值将保持不变.

A prvalue of type "pointer to cv1 void" can be converted to a prvalue of type "pointer to cv2 T", where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b. Otherwise, the pointer value is unchanged by the conversion.

我想知道,转换是否可以指针互转换有什么区别?有没有一种情况,当将void *强制转换为指针可互转换的对象时,实际上会更改指针值吗?这种区别的目的是什么?

I wonder, what is the difference whether the conversion is pointer-interconvertible or not? Is there a case, when casting a void * to something pointer-interconvertible actually changes the pointer value? What is the intent of this distinction?

出于完整性考虑,指针可互换:

如果满足以下条件,则两个对象a和b是指针可互换的:

Two objects a and b are pointer-interconvertible if:

  • (4.1)它们是同一对象,或者
  • (4.2)一个是联合对象,另一个是该对象的非静态数据成员([class.union]),或者
  • (4.3)一个是标准布局类对象,另一个是该对象的第一个非静态数据成员,或者,如果该对象没有 非静态数据成员,该对象的任何基类子对象 ([class.mem]),或
  • (4.4)存在一个对象c,使得a和c是指针可互换的,而c和b是指针可互换的.
  • (4.1) they are the same object, or
  • (4.2) one is a union object and the other is a non-static data member of that object ([class.union]), or
  • (4.3) one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object has no non-static data members, any base class subobject of that object ([class.mem]), or
  • (4.4) there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.

如果两个对象是指针可互换的,则它们具有相同的 地址,并且可以从指针获取指向一个的指针 通过reinterpret_cast传递给对方.

If two objects are pointer-interconvertible, then they have the same address, and it is possible to obtain a pointer to one from a pointer to the other via a reinterpret_­cast.

推荐答案

您可能会误解术语指针值".该术语在 [basic.compound]/3 中定义:

You may misunderstand the term "pointer value". The term is defined in [basic.compound]/3:

指针类型的每个值都是以下之一:

Every value of pointer type is one of the following:

  • 指向对象或函数的指针(指向该对象或函数的 point 指针),或

位于对象([expr.add])末尾的指针,或者

a pointer past the end of an object ([expr.add]), or

该类型的空指针值([conv.ptr]),或

the null pointer value ([conv.ptr]) for that type, or

一个无效的指针值.

指针类型的值,它是指向或超过对象末尾的指针.表示对象或内存占用的内存([intro.memory])中第一个字节的地址.对象所占存储空间结束后的第一个字节.

A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory ([intro.memory]) occupied by the object or the first byte in memory after the end of the storage occupied by the object, respectively.

因此您可以看到标准中的术语指针值"是一个非常抽象的术语.即使两个指针值表示相同的地址,它们也可能具有不同的值. cppreference 中的示例很好地演示了指针值"的概念:

So you can see the term "pointer value" in the standard is a very abstract term. Even if two pointer values represent the same address, they may have different values. The example in cppreference demonstrates the concept of "pointer value" nicely:

struct S1 { int a; } s1;
struct S2 { int a; private: int b; } s2; // not standard-layout
union U { int a; double b; } u = {0};
int arr[2];

int* p1 = reinterpret_cast<int*>(&s1); // value of p1 is "pointer to s1.a" because s1.a
                                       // and s1 are pointer-interconvertible

int* p2 = reinterpret_cast<int*>(&s2); // value of p2 is unchanged by reinterpret_cast and
                                       // is "pointer to s2". 

int* p3 = reinterpret_cast<int*>(&u);  // value of p3 is "pointer to u.a": u.a and u are
                                       // pointer-interconvertible

double* p4 = reinterpret_cast<double*>(p3); // value of p4 is "pointer to u.b": u.a and u.b
                                            // are pointer-interconvertible because both
                                            // are pointer-interconvertible with u

int* p5 = reinterpret_cast<int*>(&arr); // value of p5 is unchanged by reinterpret_cast and
                                        // is "pointer to arr"

这篇关于static_cast的指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆