Saturday, May 29, 2010

String Insert Ambiguity

If s is a string, then the following code may not compile:
    s.insert(0,1,' ');
because there is an ambiguity between the following two member functions:
    insert(size_type idx, size_type num, charT c);
    insert(iterator  pos, size_type num, charT c);
when iterator is implemented as char*.
Note that 0 is a valid size_type and a valid char*.
    s.insert(static_cast(0),1,' ');
would fix the ambiguity.

Reference: The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 491.

No comments:

Post a Comment