简体   繁体   English

intptr_t是uintptr_t的签名对应物(反之亦然)?

[英]Is intptr_t a signed counterpart of uintptr_t (and vice versa)?

I'm developing some tests for the add_signed MPL class that converts the type to its signed counterpart. 我正在为add_signed MPL类开发一些测试,它将类型转换为已签名的对应类型。 It is defined as follows: 它的定义如下:

template<class T>
struct add_signed { 
    typedef T type;
};

template<>
struct add_signed<std::uint8_t> { 
    typedef std::int8_t type;
};

template<>
struct add_signed<std::uint16_t> { 
    typedef std::int16_t type;
};

template<>
struct add_signed<std::uint32_t> { 
    typedef std::int32_t type;
};

template<>
struct add_signed<std::uint64_t> { 
    typedef std::int64_t type;
};

While testing on different types I noticed that the following evaluates to true: 在对不同类型进行测试时,我注意到以下评估结果为true:

std::is_same<add_signed<uintptr_t>::type, intptr_t>::value  // true

Similarly for the add_unsigned MPL class, the following code evaluates to true: 类似地,对于add_unsigned MPL类,以下代码的计算结果为true:

std::is_same<add_unsigned<intptr_t>::type, uintptr_t>::value  // true

My compiler is MSVC 2010. 我的编译器是MSVC 2010。

So the question is - can we assume that in all (sane) compilers signing intptr_t will produce uintptr_t and vice versa? 所以问题是 - 我们可以假设在所有(理智的)编译器中签名intptr_t会产生uintptr_t,反之亦然吗?

The types intptr_t and uintptr_t are optional in ISO/IEC 9899:1999 (C99), but where one is implemented, so is the other. intptr_tuintptr_t在ISO / IEC 9899:1999(C99)中是可选的,但是其中一个是实现的,另一个是实现的。 All signed types have an unsigned counterpart of the same size, and vice versa. 所有签名类型都具有相同大小的无符号对应项,反之亦然。

§7.18.1 Integer types §7.18.1整数类型

When typedef names differing only in the absence or presence of the initial u are defined, they shall denote corresponding signed and unsigned types as described in 6.2.5; 当typedef名称仅在初始u的存在或不存在时有所不同时,它们应表示6.2.5中描述的相应的有符号和无符号类型; an implementation providing one of these corresponding types shall also provide the other. 提供这些相应类型之一的实现也应提供另一种。

... ...

§7.18.1.4 Integer types capable of holding object pointers §7.18.1.4能够保存对象指针的整数类型

The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer: 以下类型指定一个带符号的整数类型,其属性是任何有效的void指针都可以转换为此类型,然后转换回指向void的指针,结果将等于原始指针:

 intptr_t 

The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer: 以下类型指定一个无符号整数类型,其属性是任何有效的void指针都可以转换为此类型,然后转换回指向void的指针,结果将与原始指针进行比较:

  uintptr_t 

These types are optional. 这些类型是可选的。

Note that within the meaning of the C standard, functions are not objects; 注意,在C标准的含义内,函数不是对象; it is not guaranteed by the C standard that uintptr_t can hold a function pointer. C标准不保证uintptr_t可以保存函数指针。

Fortunately, POSIX steps to the rescue: it does require that object pointers and function pointers are the same size. 幸运的是, POSIX采取措施:它确实需要对象指针和函数指针的大小相同。

2.12.3 Pointer Types 2.12.3指针类型

All function pointer types shall have the same representation as the type pointer to void . 所有函数指针类型应与void指向的类型指针具有相同的表示形式。 Conversion of a function pointer to void * shall not alter the representation. 将函数指针转换为void *不得改变表示。 A void * value resulting from such a conversion can be converted back to the original function pointer type, using an explicit cast, without loss of information. 这种转换产生的void *值可以使用显式转换转换回原始函数指针类型,而不会丢失信息。

Note: 注意:

The ISO C standard does not require this, but it is required for POSIX conformance. ISO C标准不要求这样,但它是POSIX一致性所必需的。

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

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