是否存在阻止内置函数拥有静态成员的技术限制?
我觉得这很残暴:
std::numeric_limits<int>::max()
我真的希望我能这样写:
int::max
是的,有 INT_MAX
和朋友。但有时您正在处理类似 streamsize
的内容,它是未指定的内置函数的同义词,因此您不知道是否应该使用 INT_MAX
还是 LONG_MAX
或其他。是否存在技术限制阻止将诸如 int::max
之类的内容放入该语言中?或者只是除了我之外没有人对此感兴趣?
I find this atrocious:
std::numeric_limits<int>::max()
And really wish I could just write this:
int::max
Yes, there is INT_MAX
and friends. But sometimes you are dealing with something like streamsize
, which is a synonym for an unspecified built-in, so you don't know whether you should use INT_MAX
or LONG_MAX
or whatever. Is there a technical limitation that prevents something like int::max
from being put into the language? Or is it just that nobody but me is interested in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原始类型不是类类型,因此它们没有静态成员,仅此而已。
如果你将它们设置为类类型,那么你正在改变语言的基础(尽管考虑一下,出于兼容性原因,这不会是一个问题,更像是标准人员要弄清楚的一些令人头疼的问题)到底要添加哪些成员)。
但更重要的是,我认为除了你之外没有人对此感兴趣:) ;就我个人而言,我并不觉得
numeric_limits
如此残暴(实际上,它非常 C++ 风格 - 尽管许多人可能会认为 C++ 风格的东西通常看起来很残暴 :P )。总而言之,我想说这是通常的 “每个功能都以负 100 分开头” 点;这篇文章讨论了 C#,但它与 C++ 更相关,C++ 已经拥有大量的语言功能和微妙之处、复杂的标准和许多可以否决的编译器供应商:
即使该提案是由其他人精心准备的,标准委员会仍然需要时间来审查和讨论它,并且它很可能会被拒绝,因为它会重复已经可以毫无问题的东西。
Primitive types are not class types, so they don't have static members, that's it.
If you make them class types, you are changing the foundations of the language (although thinking about it it wouldn't be such a problem for compatibility reasons, more like some headaches for the standard guys to figure out exactly what members to add to them).
But more importantly, I think that nobody but you is interested in it :) ; personally I don't find
numeric_limits
so atrocious (actually, it's quite C++-ish - although many can argue that often what is C++-ish looks atrocious :P ).All in all, I'd say that this is the usual "every feature starts with minus 100 points" point; the article talks about C#, but it's even more relevant for C++, that has already tons of language features and subtleties, a complex standard and many compiler vendors that can put their vetoes:
Even if the proposal were carefully prepared by someone else, it would still take time for the standard committee to examine and discuss it, and it would probably be rejected because it would be a duplication of stuff that is already possible without problems.
实际上存在多个问题:
也就是说,如果您觉得需要更短的符号,只需创建它:
There are actually multiple issues:
That said, if you feel you want shorter notation for this, just create it:
为什么不使用 std::numeric_limits::max() ?至于为什么它是一个函数(max())而不是一个常数(max),我不知道。在我自己的应用程序中,我创建了自己的 num_traits 类型,它提供最大值作为静态常量而不是函数(并且提供比 numeric_limits 更多的信息)。
如果他们在“int”本身上定义一些常量和函数,那就太好了,就像 C# 的 int.MaxValue、int.MaxValue 和 int.Parse(string) 一样,但这并不是 C++ 委员会决定的。
Why don't you use
std::numeric_limits<streamsize>::max()
? As for why it's a function (max()) instead of a constant (max), I don't know. In my own app I made my own num_traits type that provides the maximum value as a static constant instead of a function, (and provides significantly more information than numeric_limits).It would be nice if they had defined some constants and functions on "int" itself, the way C# has int.MaxValue, int.MaxValue and int.Parse(string), but that's just not what the C++ committee decided.