C++ Standard Library
  1. C++ Standard Library
  2. STDCXX-1009

std::string::{assign|append|insert|replace} ([iterator, iterator,] size_type, value_type) ambiguous

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 4.1.2, 4.1.3, 4.1.4, 4.2.0, 4.2.1
    • Fix Version/s: 4.2.2
    • Component/s: 21. Strings
    • Labels:
      None
    • Environment:

      All

    • Patch Info:
      Patch Available
    • Regression:
      Regression
    • Severity:
      Compiler Error

      Description

      The following test case fails to compile:

      test.cpp
      #include <string>
      
      int main ()
      {
          char c = 5;
          std::string s (4, '0' + c);
          s.assign (4, '0' + c);
          s.append (4, '0' + c);
          s.insert (s.begin (), 4, '0' + c);
          s.replace (s.begin (), s.end (), 4, '0' + c);
      
          return 0;
      }
      
      test.cpp
      D:\_Libs\stdcxx-4.2.2\include\string(370) : error C2666: 'std::basic_string<_CharT,_Traits,_Allocator>::assign' : 2 overloads have similar conversions
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(381): could be 'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::assign(std::basic_string<_CharT,_Traits,_Allocator>::size_type,std::basic_string<_CharT,_Traits,_Allocator>::value_type,int)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(374): or       'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::assign<_InputIter>(_InputIter,_InputIter,void *)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
              while trying to match the argument list '(int, int, int)'
              test.cpp(7) : see reference to function template instantiation 'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::assign<int>(_InputIter,_InputIter)' being compiled
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
      D:\_Libs\stdcxx-4.2.2\include\string(320) : error C2666: 'std::basic_string<_CharT,_Traits,_Allocator>::append' : 2 overloads have similar conversions
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(330): could be 'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::append(std::basic_string<_CharT,_Traits,_Allocator>::size_type,std::basic_string<_CharT,_Traits,_Allocator>::value_type,int)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(324): or       'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::append<_InputIter>(_InputIter,_InputIter,void *)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
              while trying to match the argument list '(int, int, int)'
              test.cpp(8) : see reference to function template instantiation 'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::append<int>(_InputIter,_InputIter)' being compiled
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
      D:\_Libs\stdcxx-4.2.2\include\string(422) : error C2666: 'std::basic_string<_CharT,_Traits,_Allocator>::insert' : 2 overloads have similar conversions
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(432): could be 'void std::basic_string<_CharT,_Traits,_Allocator>::insert(std::basic_string<_CharT,_Traits,_Allocator>::iterator,std::basic_string<_CharT,_Traits,_Allocator>::size_type,std::basic_string<_CharT,_Traits,_Allocator>::value_type,int)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(426): or       'void std::basic_string<_CharT,_Traits,_Allocator>::insert<_InputIter>(std::basic_string<_CharT,_Traits,_Allocator>::iterator,_InputIter,_InputIter,void *)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
              while trying to match the argument list '(std::basic_string<_CharT,_Traits,_Allocator>::iterator, int, int, int)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              test.cpp(9) : see reference to function template instantiation 'void std::basic_string<_CharT,_Traits,_Allocator>::insert<int>(std::basic_string<_CharT,_Traits,_Allocator>::iterator,_InputIter,_InputIter)' being compiled
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
      D:\_Libs\stdcxx-4.2.2\include\string(616) : error C2666: 'std::basic_string<_CharT,_Traits,_Allocator>::replace' : 2 overloads have similar conversions
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string(605): could be 'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::replace(std::basic_string<_CharT,_Traits,_Allocator>::iterator,std::basic_string<_CharT,_Traits,_Allocator>::iterator,std::basic_string<_CharT,_Traits,_Allocator>::size_type,std::basic_string<_CharT,_Traits,_Allocator>::value_type,int)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              D:\_Libs\stdcxx-4.2.2\include\string.cc(493): or       'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::replace<_InputIter>(std::basic_string<_CharT,_Traits,_Allocator>::iterator,std::basic_string<_CharT,_Traits,_Allocator>::iterator,_InputIter,_InputIter,void *)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
              while trying to match the argument list '(std::basic_string<_CharT,_Traits,_Allocator>::iterator, std::basic_string<_CharT,_Traits,_Allocator>::iterator, int, int, int)'
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              and
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>
              ]
              test.cpp(10) : see reference to function template instantiation 'std::basic_string<_CharT,_Traits,_Allocator> &std::basic_string<_CharT,_Traits,_Allocator>::replace<int>(std::basic_string<_CharT,_Traits,_Allocator>::ite
      rator,std::basic_string<_CharT,_Traits,_Allocator>::iterator,_InputIter,_InputIter)' being compiled
              with
              [
                  _CharT=char,
                  _Traits=std::char_traits<char>,
                  _Allocator=std::allocator<char>,
                  _InputIter=int
              ]
      

      The proposed patch:

      string.diff
      Index: include/string
      ===================================================================
      --- include/string	(revision 691313)
      +++ include/string	(working copy)
      @@ -327,9 +327,10 @@
                               __first, __last), *this;
           }
       
      -    basic_string& append (size_type __n, value_type __c, int) {
      +    template <class _IntType>
      +    basic_string& append (_IntType __n, _IntType __c, int) {
               // unnamed arg is used for overload resolution
      -        return append (__n, __c);
      +        return append (size_type (__n), value_type (__c));
           }
       
       #else   // if defined (_RWSTD_NO_MEMBER_TEMPLATES)
      @@ -378,9 +379,11 @@
                               _C_make_iter (_C_data + size ()), __first, __last);
           }
       
      -    basic_string& assign (size_type __n, value_type __c, int) {
      +    template <class _IntType>
      +    basic_string& assign (_IntType __n, _IntType __c, int) {
               // unnamed arg is used for overload resolution
      -        return replace (size_type (), size (), __n, __c);
      +        return replace (size_type (), size (),
      +                        size_type (__n), value_type (__c));
           }
       
       #else   // if defined (_RWSTD_NO_MEMBER_TEMPLATES)
      @@ -429,9 +432,11 @@
               replace (__p, __p, __first, __last);
           }
       
      -    void insert (iterator __p, size_type __n, value_type __c, int) {
      +    template <class _IntType>
      +    void insert (iterator __p, _IntType __n, _IntType __c, int) {
               // unnamed arg is used for overload resolution
      -        replace (_C_off (__p), size_type (), __n, __c);
      +        replace (_C_off (__p), size_type (),
      +                 size_type (__n), value_type (__c));
           }
       
           void insert (iterator __p,
      @@ -601,10 +606,12 @@
       
       #  endif   // _RWSTD_NO_STRING_OUTLINED_MEMBER_TEMPLATES
       
      +    template <class _IntType>
           basic_string&
           replace (iterator __first, iterator __last,
      -             size_type __n, value_type __c, int) {
      -        return replace (_C_off (__first), _C_off (__first, __last), __n, __c);
      +             _IntType __n, _IntType __c, int) {
      +        return replace (_C_off (__first), _C_off (__first, __last),
      +                        size_type (__n), value_type (__c));
           }
       
           // 21.3.5.6, p21
      

        Issue Links

          Activity

          No work has yet been logged on this issue.

            People

            • Assignee:
              Farid Zaripov
              Reporter:
              Farid Zaripov
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified

                  Development