简体   繁体   English

`std::is_same_v<size_t, uint64_t> 当两种类型都是 8 字节长时,` 评估为 `false`</size_t,>

[英]`std::is_same_v<size_t, uint64_t>` evaluates to `false` when both types are 8 bytes long

Code:代码:

#include <iostream>
#include <type_traits>

int main() {
  std::cout << "sizeof(size_t): " << sizeof(size_t) << std::endl;
  std::cout << "sizeof(uint64_t): " << sizeof(uint64_t) << std::endl;
  if constexpr (std::is_same_v<size_t, uint64_t>) {
    std::cout << "size_t == uint64_t" << std::endl;
  } else {
    std::cout << "size_t != uint64_t" << std::endl;
  }
}

Result:结果:

sizeof(size_t): 8
sizeof(uint64_t): 8
size_t != uint64_t

Compiler Info:编译器信息:

Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix

Is there a reason why size_t doesn't equal uint64_t ? size_t不等于uint64_t有什么原因吗? If so, is there a particular mention in the standard of why this is so?如果是这样,标准中是否特别提到了为什么会这样?

Usually the type size_t is an alias for the type unsigned long .通常类型size_t是类型unsigned long的别名。

From the C Standard (7.19 Common definitions <stddef.h>)来自 C 标准(7.19 通用定义<stddef.h>)

4 The types used for size_t and ptrdiff_t should not have an integer conversion rank greater than that of signed long int unless the implementation supports objects large enough to make this necessary. 4用于 size_t 和 ptrdiff_t 的类型的 integer 转换等级不应大于signed long int 的转换等级,除非实现支持足够大的对象以使其成为必要。

Pay attention to that the rank of the type unsigned long int is equal to the rank of the type signed long int .请注意, unsigned long int类型的等级等于signed long int类型的等级。

On the other hand, the type uint64_t usually is defined as an alias for the type unsigned long long int .另一方面,类型uint64_t通常被定义为类型unsigned long long int的别名。 Though that is implementation defined.虽然这是实现定义的。

As for your code then the shown output means that sizeof( unsigned long int ) also is equal to 8 on the used system.至于您的代码,那么显示的 output 意味着sizeof( unsigned long int )在使用的系统上也等于 8。

  1. The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object ([expr.sizeof]). size_t 类型是实现定义的无符号 integer 类型,它大到足以包含任何 object ([expr.sizeof]) 的字节大小。
  2. Recommended practice: An implementation should choose types for ptrdiff_t and size_t whose integer conversion ranks ([conv.rank]) are no greater than that of signed long int unless a larger size is necessary to contain all the possible values.推荐实践:实现应该为 ptrdiff_t 和 size_t 选择其 integer 转换等级 ([conv.rank]) 不大于signed long int 的类型,除非需要更大的大小来包含所有可能的值。

[support.types] [支持.types]

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

相关问题 为什么32位系统上的std :: size_t 4个字节,当无符号长long在32位和64位系统上都是8个字节时? - Why is std::size_t 4 bytes on 32bit systems when unsigned long long is 8 bytes on both 32bit and 64 bit systems? 为什么在 Mac OS X 上使用 size_t 时 uint32_t 和 uint64_t 之间存在歧义? - Why is there ambiguity between uint32_t and uint64_t when using size_t on Mac OS X? C ++:有没有理由使用uint64_t而不是size_t - C++: Is there any reason to use uint64_t instead of size_t 将uint64_t转换为字节时,从不同大小的整数转换为指针 - cast to pointer from integer of different size when converting uint64_t to bytes 将uint64_t转换为std :: string - Convert uint64_t to std::string std :: string到uint64_t - std::string to a uint64_t 如何将字节从 uint64_t 转换为 double? - How to convert bytes from uint64_t to double? 不能将 uint64_t 与 rdrand 一起使用,因为它需要 unsigned long long,但 uint64_t 被定义为 unsigned long - Can't use uint64_t with rdrand as it expects unsigned long long, but uint64_t is defined as unsigned long 将二进制字节(char *)转换为uint64_t *,是否安全? - casting binary bytes (char*) to uint64_t*, is it safe? GCC: 'std::is_same_v<int, t> ' 在常量表达式中不可用</int,> - GCC: 'std::is_same_v<int, T>' is not usable in a constant expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM