It is interesting to note that the form of append here;
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 );.
The string class is large and it is possible that the particular use case (and overload) for 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).
There are several alternatives to the append adding a single character.
- Adding a string containing a single character can be done
str.append("b");. Albeit that this not exactly the same, it has the same effect.
- As mentioned there is
operator+=
- There is also
push_back(), which is consistent with other standard containers
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.
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; one possible solution could have been to reverse the order of the count and char (possibly adding a default);
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.
std::string::append(char). Use+=if you want to do that...std::string::append(count, char)withcount=1orstd::string::push_back(char)charis just another integer type. On certain platforms,sizeof( char )andsizeof( int )might actually be identical. Look at the list ofstd::stringconstructors, andappend()overloads. Having an overload that takes a single integer value might be somewhat error-prone -- and you do have the sematically identicalstd::string( "b" ),append( "b" ),operator+=( "b" ), andoperator+=( 'b' ).+=has only the one integer overload.