* Making strings of arbitrary character types @ 2004-12-01 9:23 Nick NoSpam 2004-12-01 9:39 ` Paolo Carlini 0 siblings, 1 reply; 9+ messages in thread From: Nick NoSpam @ 2004-12-01 9:23 UTC (permalink / raw) To: gcc I read the section: "Making strings of arbitrary character types" (http://gcc.gnu.org/onlinedocs/libstdc ++/21_strings/howto.html#5) ; very helpful. With GCC 3.3.2, the section is spot on--trying something like basic_string<unsigned char>--yields linker errors about missing char_traits for unsigned char. But with GCC 3.4.2, basic_string<unsigned char> works out of the box. Does 3.4.2 provide a specialization for this type (and possibly others)? If so, is it intentional (ie. can I expect it in future versions)? Or should I specialize char_traits for portability? -- Regards, Nick G. http://www.wheeloyum.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 9:23 Making strings of arbitrary character types Nick NoSpam @ 2004-12-01 9:39 ` Paolo Carlini 2004-12-01 10:08 ` Nick NoSpam 0 siblings, 1 reply; 9+ messages in thread From: Paolo Carlini @ 2004-12-01 9:39 UTC (permalink / raw) To: Nick NoSpam; +Cc: gcc Hi, >But with GCC 3.4.2, basic_string<unsigned char> works out of the box. > >Does 3.4.2 provide a specialization for this type (and possibly others)? >If so, is it intentional (ie. can I expect it in future versions)? Or >should I specialize char_traits for portability? > Hey, this is Free Software, no mysteries! ;) Have a look to include/bits/char_traits.h, there are plenty of interesting comments and info. You will learn that a "generalization" of char_traits is now provided, which often will work out of the box for many POD types besides char and wchar_t. However, you will also learn that some typedef and eof() are likely to be wrong, in general (are Ok for unsigned char, by the way, and other "simple" cases) Indeed, this is an *intentional* improvement, and you can expect it to be present at least in the whole 3.4.x series and the forthcoming 4.0.x series. You are also right, however, that the fully portable solution involves providing your own char_traits (probably, you can master that by studying the above file!) Thanks, Paolo. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 9:39 ` Paolo Carlini @ 2004-12-01 10:08 ` Nick NoSpam 2004-12-01 10:17 ` Nick NoSpam 0 siblings, 1 reply; 9+ messages in thread From: Nick NoSpam @ 2004-12-01 10:08 UTC (permalink / raw) To: gcc On Wed, 2004-12-01 at 10:40 +0100, Paolo Carlini wrote: > Hi, > > Have a look to include/bits/char_traits.h, there are plenty of > interesting comments > and info. You will learn that a "generalization" of char_traits is now > provided, which > often will work out of the box for many POD types besides char and wchar_t. > However, you will also learn that some typedef and eof() are likely to > be wrong, > in general (are Ok for unsigned char, by the way, and other "simple" cases) Great; thanks! Any chance this generalization can be used on 3.3.x or does the compiler have issues? > Indeed, this is an *intentional* improvement, and you can expect it to > be present at > least in the whole 3.4.x series and the forthcoming 4.0.x series. You > are also right, > however, that the fully portable solution involves providing your own > char_traits > (probably, you can master that by studying the above file!) Sounds like this is the route I'll have to take as portability is high on the priority list. Thanks for your help and the quick response! -- Regards, Nick G. http://www.wheeloyum.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 10:08 ` Nick NoSpam @ 2004-12-01 10:17 ` Nick NoSpam 2004-12-01 10:20 ` Paolo Carlini 0 siblings, 1 reply; 9+ messages in thread From: Nick NoSpam @ 2004-12-01 10:17 UTC (permalink / raw) To: gcc; +Cc: Paolo Carlini On Wed, 2004-12-01 at 11:01 +0100, Paolo Carlini wrote: > In principle, you can try: basic_string itself should be ok, but you > may see problems in other areas (like iostreams, fstreams), Right you are; I just ran across problems w/ ofstream using 3.3.x. > which, at the time, were not completely "char_traits"-general, so to > speak... Are you referring to GCC's implementation? -- Regards, Nick G. http://www.wheeloyum.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 10:17 ` Nick NoSpam @ 2004-12-01 10:20 ` Paolo Carlini 2004-12-01 16:17 ` Nick NoSpam 0 siblings, 1 reply; 9+ messages in thread From: Paolo Carlini @ 2004-12-01 10:20 UTC (permalink / raw) To: Nick NoSpam; +Cc: gcc Nick NoSpam wrote: >>which, at the time, were not completely "char_traits"-general, so to >>speak... >> >> >Are you referring to GCC's implementation? > > Right, 3.4.x is vastly better in this area (and we don't have any open bug ;) Paolo. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 10:20 ` Paolo Carlini @ 2004-12-01 16:17 ` Nick NoSpam 2004-12-01 17:57 ` Paolo Carlini 0 siblings, 1 reply; 9+ messages in thread From: Nick NoSpam @ 2004-12-01 16:17 UTC (permalink / raw) To: Paolo Carlini; +Cc: gcc On Wed, 2004-12-01 at 11:22 +0100, Paolo Carlini wrote: > Right, 3.4.x is vastly better in this area (and we don't have any open > bug ;) I hope this isn't the first. Compiling the program below w/ GCC 3.4.2 and running produces: terminate called after throwing an instance of 'std::bad_cast' what(): St8bad_cast Aborted --- CODE BEGIN --- #include <string> #include <iostream> #include <fstream> typedef unsigned char UINT8; typedef std::basic_string<UINT8> ByteArray; void writeBytes() { ByteArray bytes(30, (UINT8)'A'); std::basic_ofstream<ByteArray::value_type> fout("test.dat", std::ios::binary | std::ios::out); fout << bytes; // Exception thrown here } int main(int argc, char *argv[]) { writeBytes(); return(0); } --- CODE END --- Basically, the exception is thrown because the locale is not set. Backtrace: #0 std::__check_facet<std::codecvt<unsigned char, char, __mbstate_t> > (__f=0x0) at localefwd.h:187 #1 0x0804cfe3 in std::basic_filebuf<unsigned char, std::char_traits<unsigned char> >::xsputn (this=0xbfffef74, __s=0x805005c 'A' <repeats 30 times>, __n=30) at fstream.tcc:507 #2 0x0804ac5e in std::basic_streambuf<unsigned char, std::char_traits<unsigned char> >::sputn (this=0xbfffef74, __s=0x805005c 'A' <repeats 30 times>, __n=30) at streambuf:421 #3 0x0804a2af in std::basic_ostream<unsigned char, std::char_traits<unsigned char> >::_M_write (this=0xbfffef70, __s=0x805005c 'A' <repeats 30 times>, __n=30) at ostream:288 #4 0x08049867 in std::operator<< <unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > (__out=@0xbfffef70, __str=@0xbffff0a0) at ostream.tcc:665 #5 0x08049276 in writeBytes () at main.cpp:13 #6 0x0804930b in main (argc=1, argv=0xbffff134) at main.cpp:18 GCC -v: Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.2/specs Configured with: /var/tmp/portage/gcc-3.4.2/work/gcc-3.4.2/configure --enable-version-specific-runtime-libs --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.2/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.2/include/g ++-v3 --with-local-prefix=/usr/local --host=i686-pc-linux-gnu --disable-multilib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-shared --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --with-gnu-ld --enable-threads=posix --enable-languages=c,c++ --disable-libgcj Thread model: posix gcc version 3.4.2 -- Regards, Nick G. http://www.wheeloyum.com ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 16:17 ` Nick NoSpam @ 2004-12-01 17:57 ` Paolo Carlini 2004-12-01 18:07 ` Paolo Carlini 2004-12-02 5:35 ` Nick NoSpam 0 siblings, 2 replies; 9+ messages in thread From: Paolo Carlini @ 2004-12-01 17:57 UTC (permalink / raw) To: Nick NoSpam; +Cc: gcc Nick NoSpam wrote: >On Wed, 2004-12-01 at 11:22 +0100, Paolo Carlini wrote: > > >>Right, 3.4.x is vastly better in this area (and we don't have any open >>bug ;) >> >> >I hope this isn't the first. > No, it isn't ;) You have to prepare a suited codecvt facet, able to convert during I/O from/to you internal ("in memory") type (unsigned char) to the external ("on disk") type (char): unfortunately, there is no such as thing as a "general" codecvt... I suggest taking inspiration from the snippet present in our testsuite, in particular: testsuite/27_io/basic_filebuf/seekoff/wchar_t/12790*.cc + testsuite_character.h Paolo. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 17:57 ` Paolo Carlini @ 2004-12-01 18:07 ` Paolo Carlini 2004-12-02 5:35 ` Nick NoSpam 1 sibling, 0 replies; 9+ messages in thread From: Paolo Carlini @ 2004-12-01 18:07 UTC (permalink / raw) To: Paolo Carlini; +Cc: Nick NoSpam, gcc Paolo Carlini wrote: > I suggest taking inspiration from the snippet present in our > testsuite, in particular: > > testsuite/27_io/basic_filebuf/seekoff/wchar_t/12790*.cc Sorry, of course I meant (no wchar_t involved!): testsuite/27_io/basic_filebuf/seekoff/12790*.cc + testsuite_character.h Paolo. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Making strings of arbitrary character types 2004-12-01 17:57 ` Paolo Carlini 2004-12-01 18:07 ` Paolo Carlini @ 2004-12-02 5:35 ` Nick NoSpam 1 sibling, 0 replies; 9+ messages in thread From: Nick NoSpam @ 2004-12-02 5:35 UTC (permalink / raw) To: Paolo Carlini; +Cc: gcc On Wed, 2004-12-01 at 18:58 +0100, Paolo Carlini wrote: > You have to prepare a suited codecvt facet, able to convert during I/O > from/to > you internal ("in memory") type (unsigned char) to the external ("on > disk") type > (char): unfortunately, there is no such as thing as a "general" codecvt... That clears it up. Thanks for all your help! -- Regards, Nick G. http://www.wheeloyum.com ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2004-12-02 5:35 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2004-12-01 9:23 Making strings of arbitrary character types Nick NoSpam 2004-12-01 9:39 ` Paolo Carlini 2004-12-01 10:08 ` Nick NoSpam 2004-12-01 10:17 ` Nick NoSpam 2004-12-01 10:20 ` Paolo Carlini 2004-12-01 16:17 ` Nick NoSpam 2004-12-01 17:57 ` Paolo Carlini 2004-12-01 18:07 ` Paolo Carlini 2004-12-02 5:35 ` Nick NoSpam
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).