是否存在阻止内置函数拥有静态成员的技术限制?

发布于 2024-12-25 14:49:00 字数 357 浏览 1 评论 0原文

我觉得这很残暴:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来世叙缘 2025-01-01 14:49:00

原始类型不是类类型,因此它们没有静态成员,仅此而已。

如果你将它们设置为类类型,那么你正在改变语言的基础(尽管考虑一下,出于兼容性原因,这不会是一个问题,更像是标准人员要弄清楚的一些令人头疼的问题)到底要添加哪些成员)。

但更重要的是,我认为除了你之外没有人对此感兴趣:) ;就我个人而言,我并不觉得 numeric_limits 如此残暴(实际上,它非常 C++ 风格 - 尽管许多人可能会认为 C++ 风格的东西通常看起来很残暴 :P )。


总而言之,我想说这是通常的 “每个功能都以负 100 分开头” 点;这篇文章讨论了 C#,但它与 C++ 更相关,C++ 已经拥有大量的语言功能和微妙之处、复杂的标准和许多可以否决的编译器供应商:

实现这一目标的一种方法是通过“负 100 分”的概念。每个功能一开始的分数都是 100 分,这意味着它必须对整个包产生显着的净积极影响才能进入该语言。有些功能对于一种语言来说是可以的,只是还不够好,无法融入到该语言中。

即使该提案是由其他人精心准备的,标准委员会仍然需要时间来审查和讨论它,并且它很可能会被拒绝,因为它会重复已经可以毫无问题的东西。

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:

One way to do that is through the concept of “minus 100 points”. Every feature starts out in the hole by 100 points, which means that it has to have a significant net positive effect on the overall package for it to make it into the language. Some features are okay features for a language to have, they just aren't quite good enough to make it into the language.

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.

两仪 2025-01-01 14:49:00

实际上存在多个问题:

  1. 内置类型不是 C++ 中的类,
  2. 不能使用 C++ 中的新成员进行扩展
  3. 假设实现需要提供某些“成员”,则 :哪个?您可能需要为类型查找许多其他属性,并且使用特征可以添加它们。

也就是说,如果您觉得需要更短的符号,只需创建它:

namespace traits {
    template <typename T> constexpr T max() {
        return std::numeric_limits<T>::max();
    }
}

int m = traits::max<int>();

using namespace traits;
int n = max<int>();

There are actually multiple issues:

  1. built-in types aren't classes in C++
  2. classes can't be extended with new members in C++
  3. assuming the implementation were required to supply certain "members": which? There are lots of other attributes you might want to find for type and using traits allows for them being added.

That said, if you feel you want shorter notation for this, just create it:

namespace traits {
    template <typename T> constexpr T max() {
        return std::numeric_limits<T>::max();
    }
}

int m = traits::max<int>();

using namespace traits;
int n = max<int>();
乖不如嘢 2025-01-01 14:49:00

为什么不使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文