std :: string和多个连接 [英] std::string and multiple concatenations

查看:129
本文介绍了std :: string和多个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑该代码段,并且请假设a,b,c和d是非空字符串。

Let’s consider that snippet, and please suppose that a, b, c and d are non-empty strings.

    std::string a, b, c, d;
    d = a + b + c;

计算3 std :: string 实例,标准库实现创建第一个临时 std :: string 对象,在其内部缓冲区中复制 a b ,然后在临时字符串和 c 之间执行相同的操作。

When computing the sum of those 3 std::string instances, the standard library implementations create a first temporary std::string object, copy in its internal buffer the concatenated buffers of a and b, then perform the same operations between the temporary string and the c.

一个程序员强调,不是这个行为, operator +(std :: string,std :: string)可以定义为返回一个 std :: string_helper

A fellow programmer was stressing that instead of this behaviour, operator+(std::string, std::string) could be defined to return a std::string_helper.

这个对象的作用是延迟实际连接到它被转换为 std :: string 的那一刻。显然, operator +(std :: string_helper,std :: string)将被定义为返回相同的助手,这将记住连接执行。

This object’s very role would be to defer the actual concatenations to the moment where it’s casted into a std::string. Obviously, operator+(std::string_helper, std::string) would be defined to return the same helper, which would "keep in mind" the fact that it has an additional concatenation to carry out.

这样的行为将节省创建n-1个临时对象,分配其缓冲区,复制它们等的CPU成本。因此我的问题是:为什么它不会像这样工作?我不能想到任何缺点或限制。

Such a behavior would save the CPU cost of creating n-1 temporary objects, allocating their buffer, copying them, etc. So my question is: why doesn’t it already work like that ?I can’t think of any drawback or limitation.

推荐答案


为什么它不会这样工作?

why doesn’t it already work like that?

我只能推测为什么它最初设计的。也许字符串库的设计师根本没有想到它;也许他们认为额外的类型转换(见下文)可能会使行为在某些情况下太令人惊讶。它是最古老的C ++库之一,我们认为理所当然的许多智慧在过去几十年里并不存在。

I can only speculate about why it was originally designed like that. Perhaps the designers of the string library simply didn't think of it; perhaps they thought the extra type conversion (see below) might make the behaviour too surprising in some situations. It is one of the oldest C++ libraries, and a lot of wisdom that we take for granted simply didn't exist in past decades.

至于为什么没有被改变为这样工作:它可能打破现有代码,通过添加一个额外的用户定义的类型转换。隐式转化最多只能涉及一个用户定义的转化。这由C ++ 11,13.3.3.1.2 / 1指定:

As to why it hasn't been changed to work like that: it could break existing code, by adding an extra user-defined type conversion. Implicit conversions can only involve at most one user-defined conversion. This is specified by C++11, 13.3.3.1.2/1:


用户定义的转换序列由初始标准

A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion followed by a second standard conversion sequence.

请考虑以下事项:

struct thingy {
    thingy(std::string);
};

void f(thingy);

f(some_string + another_string);

如果 some_string + another_string std :: string 。这可以通过转换构造函数隐式转换为 thingy 。但是,如果我们改变 operator + 的定义来给另一个类型,那么它需要两次转换( string_helper string thingy ),因此将无法编译。

This code is fine if the type of some_string + another_string is std::string. That can be implicitly converted to thingy via the conversion constructor. However, if we were to change the definition of operator+ to give another type, then it would need two conversions (string_helper to string to thingy), and so would fail to compile.

所以,如果字符串构建的速度很重要,你需要使用替换方法,例如连接 + = 。或者,根据Matthieu的回答,不要担心它,因为C ++ 11以不同的方式修复了低效率。

So, if the speed of string building is important, you'll need to use alternative methods like concatenation with +=. Or, according to Matthieu's answer, don't worry about it because C++11 fixes the inefficiency in a different way.

这篇关于std :: string和多个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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