gpt4 book ai didi

c++ - 为什么指针类型在 C++ 中不受限制?

转载 作者:太空狗 更新时间:2023-10-29 20:25:41 25 4
gpt4 key购买 nike

我认为指针的最大值是所用指针大小的最大可能值,但显然不是。

#include <iostream>
#include <limits>

int main() {
std::cout << std::numeric_limits<char *>::is_bounded << std::endl;
std::cout << (void *)std::numeric_limits<char *>::min() << std::endl;
std::cout << (void *)std::numeric_limits<char *>::max() << std::endl;
}

我用 g++ 编译并得到以下输出:

0
0
0

标准说如果 is_bounded 不为假,则 min() 和 max() 是有意义的。它还指出,如果该类型可表示的值集是有限的,则 is_bounded 应该为真。

指针不是有限的吗?为什么我的程序中 is_bounded false 和 max() 为零?

最佳答案

指针不是算术类型。

来自 18.3.2.1 类模板 numeric_limits

The numeric_limits class template provides a program with information about various properties of the implementation’s representation of the arithmetic types. Specializations shall be provided for each arithmetic type, both floating point and integer, including bool. The member is_specialized shall be true for all such specializations of numeric_limits. For all members declared static constexpr in the numeric_limits template, specializations shall define these values in such a way that they are usable as constant expressions. Non-arithmetic standard types, such as complex<T> (26.4.2), shall not have specializations.

因此。您应用非算术类型的测试会导致无意义的默认值。

关于c++ - 为什么指针类型在 C++ 中不受限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23140837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com