[英]Why is std::string::append() less powerful than std::string::operator+()?
I noticed that 我注意到了
std::string str;
str += 'b'; // works
str.append('b'); // does not work
str.append(1, 'b'); // works, but not as nice as the previous
Is there any reason why the append
method does not support a single character to be appended? 是否有任何理由为什么
append
方法不支持append
单个字符? I assumed that the operator+=
is actually a wrapper for the append
method, but this does not seem to be the case. 我假设
operator+=
实际上是append
方法的包装器,但似乎并非如此。
I figure that operator+=()
is intended to handle all the simple cases (taking only one parameter), while append()
is for things that require more than one parameter. 我认为
operator+=()
旨在处理所有简单情况(只接受一个参数),而append()
用于需要多个参数的东西。
I am actually more surprised about the existence of the single-parameter append( const basic_string& str )
and append( const CharT* s )
than about the absence of append( CharT c )
. 实际上,我对单参数
append( const basic_string& str )
和append( const CharT* s )
的存在感到更加惊讶,而不是缺少 append( CharT c )
。
Also note my comment above: char
is just an integer type. 另请注意我上面的评论:
char
只是一个整数类型。 Adding a single-parameter, integer-type overload to append()
-- or the constructor (which, by design, have several integer-type overloads already) might introduce ambiguity. 为
append()
添加单参数整数类型重载 - 或者构造函数(根据设计,已经有几个整数类型重载)可能会引入歧义。
Unless somebody finds some written rationale, or some committee members post here what they remember about the discussion, that's probably as good an explanation as any. 除非有人找到一些书面理由,或者某些委员会成员在这里发布了他们对讨论的记忆,否则这可能是一个很好的解释。
It is interesting to note that the form of append
here; 有趣的是要注意这里的
append
形式;
string& append( size_type count, CharT ch );
Mirrors the constructor taking similar input . 镜像构造函数采用类似的输入 。
basic_string( size_type count,
CharT ch,
const Allocator& alloc = Allocator() );
And some other methods that take a count
with a character, such as resize( size_type count, CharT ch );
以及其他一些使用字符
count
方法,例如resize( size_type count, CharT ch );
. 。
The string class is large and it is possible that the particular use case (and overload) for str.append('b');
字符串类很大,可能是
str.append('b');
的特定用例(和重载str.append('b');
was not considered, or the alternatives were considered sufficient. 未考虑,或替代品被认为是充分的。
Just simple the introduction of a single overload for this could introduce ambiguity if the integrals int
and char
correspond (on some platforms this may be the case). 简单地说,如果积分
int
和char
对应,则引入单个重载可能会引入歧义(在某些平台上可能就是这种情况)。
There are several alternatives to the append
adding a single character. 添加单个字符的
append
有几种替代方法。
str.append("b");
str.append("b");
. operator+=
operator+=
push_back()
, which is consistent with other standard containers push_back()
,它与其他标准容器一致 Point is, it was probably never considered as a use case (or strong enough use case), thus, a suitable overload/signature was not added to append
to cater for it. 重点是,它可能从未被视为用例(或足够强大的用例),因此,没有添加合适的重载/签名来
append
以满足它。
Alternative designs could be debated, but given the maturity of the standard and this class, it is unlikely they will be changed soon - it could very well break a lot of code. 替代设计可能会有争议,但考虑到标准和本课程的成熟度,它们不太可能很快就会改变 - 它很可能会破坏很多代码。
Alternate signatures for append
could also be considered; 也可以考虑
append
替代签名; one possible solution could have been to reverse the order of the count
and char
(possibly adding a default); 一种可能的解决方案可能是颠倒
count
和char
的顺序(可能添加默认值);
string& append(CharT ch, size_type count = 1);
Another, as described in some of the critique of basic_string
is to remove append
, there are many methods to achieve what it does. 另外,正如一些对
basic_string
的批评所描述的那样是删除append
,有很多方法可以实现它的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.