[英]`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。
- 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]) 的字节大小。
- 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 的类型,除非需要更大的大小来包含所有可能的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.