Std :: basic_stringstream <unsigned char> will not compile with MSVC 10

I am trying to get UTF-8 characters to coexist with 8-bit ANSI characters. My strategy was to represent utf-8 characters as unsigned char, so for two types of characters you can use the appropriate function overloads.

eg.

    namespace MyStuff
    {
        typedef uchar utf8_t;
        typedef std::basic_string<utf8_t> U8string;
    }
    void SomeFunc(std::string &s);
    void SomeFunc(std::wstring &s);
    void SomeFunc(MyStuff::U8string &s);

All this works very well until I try to use a string stream.

    std::basic_ostringstream<MyStuff::utf8_t> ostr;
    ostr << 1;

MSVC Visual C ++ Express V10 will not compile this:

c:\program files\microsoft visual studio 10.0\vc\include\xlocmon(213): 
    warning C4273: 'id' : inconsistent dll linkage
    c:\program files\microsoft visual studio 10.0\vc\include\xlocnum(65) : 
            see previous definition of 
            'public: static std::locale::id std::numpunct<unsigned char>::id'
    c:\program files\microsoft visual studio 10.0\vc\include\xlocnum(65) : 
            while compiling class template static data member 'std::locale::id 
            std::numpunct<_Elem>::id'
    with
    [
        _Elem=Tk::utf8_t
    ]
    c:\program files\microsoft visual studio 10.0\vc\include\xlocnum(1149) : 
        see reference to function template instantiation 
        'const _Facet &std::use_facet<std::numpunct<_Elem>>(const std::locale &)' 
        being compiled
    with
    [
        _Facet=std::numpunct<Tk::utf8_t>,
        _Elem=Tk::utf8_t
    ]
    c:\program files\microsoft visual studio 10.0\vc\include\xlocnum(1143) : 
        while compiling class template member function 
        'std::ostreambuf_iterator<_Elem,_Traits> 
        std::num_put<_Elem,_OutIt>::
        do_put(_OutIt,std::ios_base &,_Elem,std::_Bool) const'
    with
    [
        _Elem=Tk::utf8_t,
        _Traits=std::char_traits<Tk::utf8_t>,
        _OutIt=std::ostreambuf_iterator<Tk::utf8_t,std::char_traits<Tk::utf8_t>>
    ]
    c:\program files\microsoft visual studio 10.0\vc\include\ostream(295) : 
        see reference to class template instantiation 'std::num_put<_Elem,_OutIt>' 
        being compiled
    with
    [
        _Elem=Tk::utf8_t,
        _OutIt=std::ostreambuf_iterator<Tk::utf8_t,std::char_traits<Tk::utf8_t>>
    ]
    c:\program files\microsoft visual studio 10.0\vc\include\ostream(281) : 
        while compiling class template member function 
        'std::basic_ostream<_Elem,_Traits> &
        std::basic_ostream<_Elem,_Traits>::operator <<(int)'
    with
    [
        _Elem=Tk::utf8_t,
        _Traits=std::char_traits<Tk::utf8_t>
    ]
    c:\program files\microsoft visual studio 10.0\vc\include\sstream(526) : 
        see reference to class template instantiation 
        'std::basic_ostream<_Elem,_Traits>' being compiled
    with
    [
        _Elem=Tk::utf8_t,
        _Traits=std::char_traits<Tk::utf8_t>
    ]
    c:\users\michael\dvl\tmp\console\console.cpp(23) : 
        see reference to class template instantiation 
        'std::basic_ostringstream<_Elem,_Traits,_Alloc>' being compiled
    with
    [
        _Elem=Tk::utf8_t,
        _Traits=std::char_traits<Tk::utf8_t>,
        _Alloc=std::allocator<uchar>
    ]

.

c:\program files\microsoft visual studio 10.0\vc\include\xlocmon(213): 
    error C2491: 'std::numpunct<_Elem>::id' : definition of dllimport 
    static data member not allowed
    with
    [
        _Elem=Tk::utf8_t
    ]

Any ideas?

** Edited June 19, 2012 **

Well, I came closer to understanding this, but not how to solve it.

As we all know, static class variables are defined twice:

  • once in a class definition and
  • outside the class definition that sets up storage space.

eg.

    // in .h file
    class CFoo
    {
        // ...
        static int x;
    };

    // in .cpp file
    int CFoo::x = 42;

VC10 - :

    template<class _Elem>
    class numpunct : public locale::facet
    {
        // ...
        _CRTIMP2_PURE static locale::id id;
        // ...
    }

, _CRTIMP2_PURE __declspec(dllimport), , dll.

    template<class _Elem>
    locale::id numpunct<_Elem>::id;

__declspec(dllimport).

. , id dll, dll.

.

    template locale::id numpunct<char>::id;
    template locale::id numpunct<wchar_t>::id;

#if, DLL. .

. char wchar_t numpunct ARE dll

, , , id DLL, char wchar_t, , char .: - (

, , : . .

- ?

+5

All Articles