为什么std :: string不定义乘法或文字? [英] Why doesn't std::string define multiplication or literals?

查看:109
本文介绍了为什么std :: string不定义乘法或文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最初被介绍的语言中,有一个函数repeat(),它接受了一个字符串,并重复了n次.例如,repeat ("hi", 3)给出"hihihi"的结果.

In the language I was first introduced to, there was a function repeat(), that took a string, and repeated it n times. For example, repeat ("hi", 3) gives a result of "hihihi".

我确实有几次使用此函数,但令我沮丧的是,我从未在C ++中找到类似的东西.是的,我可以轻松制作自己的产品或使其更易于使用,但是令我感到惊讶的是它尚未包含在内.

I did have quite a few times that I used this function, but to my dismay, I've never found something similar in C++. Yes, I can easily make my own, or make it easier to use, but I'm kind of surprised that it isn't included already.

一个非常合适的地方是std::string:

One place it would fit in really well is in std::string:

std::string operator* (const std::string &text, int repeatCount);
std::string operator* (int repeatCount, const std::string &text);

这将允许使用以下语法:

That would allow for syntax such as:

cout << "Repeating \"Hi\" three times gives us \"" << std::string("Hi") * 3 << "\"."

现在,它本身还不是很好,但是可能会更好,这使我进入了我的另一部分:文字.

Now that in itself isn't all too great yet, but it could be better, which brings me to my other part: literals.

每次使用字符串运算符(例如operator+)时,都必须确保一个参数实际上是字符串.他们为什么不为它定义文字,如""s?不以下划线开头的文字后缀保留给实现,因此这与在任何人真正开始制作自己的字体之前可以添加它的方式应该没有冲突.

Any time we use the string operators, such as operator+, we have to make sure one argument is actually a string. Why didn't they just define a literal for it, like ""s? Literal suffixes not beginning with an underscore are reserved for the implementation, so this shouldn't conflict seeing as how this could have been added before anyone actually started making their own.

回到重复的示例,语法将简单地为:

Coming back to the repeat example, the syntax would simply be:

cout << "123"s * 3 + "456"s;

这将产生:

123123123456

与此同时,还可以包含一个用于字符的字符,以满足cout << '1's + '2's;

While at it, one for characters could be included as well, to satisfy cout << '1's + '2's;

为什么不包括这两个功能?它们绝对具有明确的含义,并且在仍然使用标准库的情况下使编码更容易.

Why weren't these two features included? They definitely have a clear meaning, and make coding easier, while still using the standard libraries.

推荐答案

关于乘法,这并不是真正的C ++理念:像Ruby之类的语言包含电池",并且具有最少惊喜的原则".他们打算将这些小细节作为最小功能中的一个. C ++是一种旨在更接近金属"的系统级语言,这一点非常清楚,因为string甚至不是该语言的核心数据类型,而是库提供的插件.甚至FORTRAN也具有本机字符串类型,因此您可以看到低级C ++的位置.这是故意的:如果您要对具有1K存储容量的嵌入式芯片进行编程而又不使用字符串怎么办?只是不包括它们.

Well, as for the multiplication, it's not really in C++'s philosophy: languages like Ruby come "batteries included" and with the "principle of least surprise". They are intended to have lots of these little niceties as an above-and-beyond-the-minimum feature. C++ is a system level language which is intended to be "closer to the metal", which is abundantly clear in that a string isn't even a core data type of the language, but a library-provided addon. Even FORTRAN has a native string type, so you can see how low-level C++ is positioned to be. This is intentional: what if you're programming an embedded chip with 1K storage and have no use for strings? Just don't include 'em.

无论如何,并不能100%知道乘法运算符应该做什么.换句话说,在C ++的核心语言和库中,除非似乎几乎每个人都同意所提议的功能,否则没有任何功能会进入.它必须真正具有普遍性.

Anyway, it's not 100% clear what the multiplication operator is supposed to do. In other words, in the core language and libraries of C++, no feature gets in unless it seems that almost everyone would agree on the proposed functionality. It's got to be really universal.

我可能会认为"123" * 3可以使"369"-将任何数字乘以3.我进一步建议,这种解释理智"到足以使您的重复运算符解释不再是唯一明确的解释.

I for one might think that "123" * 3 could give "369" - multiply any digits by 3. I further propose that this interpretation is "sane" enough to keep your repeat operator interpretation from being the only unambiguous interpretation.

文字表示法更容易回答,也更清晰:std::string是标准库的一部分,它是语言语法本身之上的抽象级别.换句话说,文字符号将在抽象级别中破灭,该抽象级别将语言功能"和可以期望与编译器捆绑在一起的库"分开.

The literal notation is far easier and more clear to answer: std::string is a part of the standard library, which is one level of abstraction above the language syntax itself. In other words, the literal notation would bust through the level of abstraction that separates "language features" and "the library that you can expect to be bundled with your compiler".

这篇关于为什么std :: string不定义乘法或文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆