Why doesn't std::string define multiplication

2019-03-06 14:51发布

问题:

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".

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.

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.

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;

This would produce:

123123123456

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.

回答1:

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.

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.

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.

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".



回答2:

std::string.append(numtimes, char to repeat); There. It does. Now, that works for character literals, as for doing that with strings there is no way to do that declaratively. Also to note, the std::string class isn't a language type, a string litteral in c++ is const char *. A pointer. Which Arrays decay into pointers, and that's how the std::string class treats what it contains , an array of characters ie char, or wchar_t for std::wstring. there templated on those types.