[bug report] no fall back implementation of char_traits<CharT>

Zhang Le ejoy@peoplemail.com.cn
Mon Feb 10 08:14:00 GMT 2003


Hi all,
   I just find in char_traits.h(gcc 3.2 shipped with rh8) there is no
   fall back implementation for _CharT. Only two explicit specialization 
   version for char and wchar_t which means by default user can only
   create basic_string<char> and basic_string<wchar_t>.
   Could you fix it?

   My program need a basic_string<short int> which fail to compile due
   to the above problem. I use the following code from stlport and it
   works.

   In addition, I also find hash<string> and hash<wstring> are missing in gcc
   3.2.

      static void  assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
      static bool  eq(const _CharT& __c1, const _CharT& __c2) 
      { return __c1 == __c2; }
      static bool  lt(const _CharT& __c1, const _CharT& __c2) 
      { return __c1 < __c2; }

      static int  compare(const _CharT* __s1, const _CharT* __s2, size_t __n) {
	      for (size_t __i = 0; __i < __n; ++__i)
		      if (!eq(__s1[__i], __s2[__i]))
			      return __s1[__i] < __s2[__i] ? -1 : 1;
	      return 0;
      }

      static size_t  length(const _CharT* __s) {
	      static const _CharT _NullChar = _CharT();
	      size_t __i;
	      for (__i = 0; !eq(__s[__i], _NullChar); ++__i)
	      {}
	      return __i;
      }

      static const _CharT*  find(const _CharT* __s, size_t __n, const _CharT& __c) {
	      for ( ; __n > 0 ; ++__s, --__n)
		      if (eq(*__s, __c))
			      return __s;
	      return 0;
      }


      static _CharT*  move(_CharT* __s1, const _CharT* __s2, size_t _Sz) {    
	      return (_Sz == 0 ? __s1 : (_CharT*)memmove(__s1, __s2, _Sz * sizeof(_CharT)));
      }

      static _CharT*  copy(_CharT* __s1, const _CharT* __s2, size_t __n) {
	      return (__n == 0 ? __s1 :
			      (_CharT*)memcpy(__s1, __s2, __n * sizeof(_CharT)));
      } 

      static _CharT*  assign(_CharT* __s, size_t __n, _CharT __c) {
	      for (size_t __i = 0; __i < __n; ++__i)
		      __s[__i] = __c;
	      return __s;
      }

      static int_type  not_eof(const int_type& __c) {
	      return !eq_int_type(__c, eof()) ? __c : int_type(0);
      }

      static char_type  to_char_type(const int_type& __c) {
	      return (char_type)__c;
      }
--
                                     Sincerely yours,
                                            Zhang Le



More information about the Gcc-bugs mailing list