tags:

views:

235

answers:

2

I need crossplatform code to skip leading spaces for wide string. It's looking that g++ (and Qt obviously) doesn't initialize slots for wide string at all So following code works fine for VC++ but almost g++ fails with bad_cast exception:

#include <string>
#include <locale>
#include <iostream>

int main()
{
    typedef std::ctype<std::wstring::value_type> vtct;
    std::wstring str=L"      1122";
    const std::wstring::value_type* unspace = 
        std::use_facet<vtct>( std::locale::classic() ).
            scan_not(std::ctype_base::space|std::ctype_base::punct, 
            str.c_str(), str.c_str() + str.length());
   //std::cout << unspace << std::endl;
   wprintf(L"{%s}\n", unspace);
   return 0;
}

According to spec: *"bad_cast exception when the facet repository in the locale object contains no facet with the requested locale::id"*

So general question how g++ deals with wide string? More narrow question - how to initialize facets at least for ctype?

Update: after some experiments, I've detected that linux g++ correctly initializes facet for wide string - so code works. Obviously described problem is feature if mingw only.

A: 

I try not to use the STL localization support because:

  • it is complicated
  • as far as I can tell, compiler support is not so good.

Since you have mentioned Qt, you can try to create a QString from the wstring and verify what chars are spaces with QChar::isSpace.

See this resource for more help with C++ localization. It includes a link to B.Stroustrup's appendix about localization.

rpg
I would jsut put (It's complicated). The second part is not true as it is not the compiler that provides localization support. It may be the libraries provided as default with your compilers do not have good support for localiztion,
Martin York
@rpg - thanks for response. it is not (-1) but: strongly disagree about complication. It is logically understendably. QChar cannot be used since code must be crossplatform, so it must be compiled as by VC++ as g++.
Dewfy
@ Martin, you are right, it's the libraries.
rpg
@Dewfy: isn't QChar supported both by g++ and VC++? I've used it both on Linux and Windows provided that I was building with the Qt libraries.
rpg
@rpg May be I didn't correctly describe the problem - there is crossplatform library out of Qt scope, it uses only stl. So QChar is not available there. When library was compiled under Qt - I've got this srange effect, (see my update) problem concerned with cuted functionality of mingw lib.
Dewfy
+1  A: 

Are you using MingGW? Their stable version don't support wchar_t.

I use C++ wide character support (facets too) in g++ on Linux without any trouble.

I had to make my code support narrow-character-only compilation mode to work with MinGW though.

Basilevs
@Basilevs see my edit bellow post - linux's g++ works very well. MinGW is used by Qt environment - that is my problem. wchar_t is defined, but multiple facets (including ctype) are not. Does anybody knows how to do it may be manually?
Dewfy
It is defined but not supported. Try version of MinGW with gcc 4.
Basilevs