可能是caseof(T *)!= sizeof(const T *)? [英] Could it be the case that sizeof(T*) != sizeof(const T*)?

查看:96
本文介绍了可能是caseof(T *)!= sizeof(const T *)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我老板讨论这个问题。他们说是的,他们可以不同。



是否可能 sizeof(T *)!= sizeof code>

解决方案

他们不能不同。对于充分不同的 T1 T2 sizeof(T1 *)可以不同于 sizeof(T2 *),但如果 T2 只是 const T1

3.9.2复合类型[basic.compound] / p>

3 [...]指向cv限定和cv非限定版本(3.9.3)的布局兼容类型的指针应具有相同的值表示和对齐要求3.11)。 [...]


任何类型 T 本身:


3.9类型[basic.types]



11如果两个类型 T1 T2 是相同类型, T1 T2 布局兼容类型。 [...]







值表示与对象表示有关,不能具有相同的值表示而不具有相同的对象表示。后者意味着需要相同的位数。

>

4 T 类型的对象的对象表示 N T 的对象占用的 c> 对象,其中 > equals sizeof(T)。对象的值表示是一组包含 T 类型的值的位。对于平凡可复制类型,值表示是对象表示中的一组位,其确定,其是实现定义的值集合的一个离散元素。 44



44)意图是C ++的内存模型与ISO / IEC 9899编程语言C的内存模型兼容。


< blockquote>

要求的要点,它不只是说两个类型具有相同的对象表示的原因是 T * const T * 不仅具有相同的位数,而且它也是 T * const T * 组成的值。这是为了保证不仅 sizeof(T *)== sizeof(const T *),但它意味着即使你可以使用 memcpy T * 指针值复制到 const T * 指针值,反之亦然并获得有意义的结果,您将获得与 const_cast 完全相同的结果。



保证也是,但是他们很复杂的解释正确,并不直接与这个问题相关,并且有一些问题在标准中破坏了一些预期的保证,所以我认为这是最好的忽略这里。


I'm arguing with my boss about this. They say "Yes they can be different."

Is it possible that sizeof(T*) != sizeof(const T*) for a type T?

解决方案

No, they can't be different. For sufficiently different T1 and T2, sizeof(T1 *) can be different from sizeof(T2 *), but if T2 is just const T1, then:

3.9.2 Compound types [basic.compound]

3 [...] Pointers to cv-qualified and cv-unqualified versions (3.9.3) of layout-compatible types shall have the same value representation and alignment requirements (3.11). [...]

And any type T is layout-compatible with itself:

3.9 Types [basic.types]

11 If two types T1 and T2 are the same type, then T1 and T2 are layout-compatible types. [...]


Value representation is in relation to the object representation, you can't have the same value representation without also having the same object representation. The latter means the same number of bits is required.

3.9 Types [basic.types]

4 The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T). The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.44

44) The intent is that the memory model of C++ is compatible with that of ISO/IEC 9899 Programming Language C.

The point of the requirement, the reason it doesn't just say that the two types have the same object representation, is that T * and const T * not only have the same number of bits, but also that it's the same bits in T * and const T * that make up the value. This is meant to guarantee not only that sizeof(T *) == sizeof(const T *), but it means even that you can use memcpy to copy a T * pointer value to a const T * pointer value or vice versa and get a meaningful result, the exact same result you would get with const_cast.

The alignment requirements provide some additional guarantees too, but they're complicated to explain properly and not directly relevant to this question, and there are issues in the standard that undermine some of the intended guarantees, so I think that's best left ignored here.

这篇关于可能是caseof(T *)!= sizeof(const T *)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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