8

我试图让 UTF-8 字符与 ANSI 8 位字符共存。我的策略是表示 utf-8 字符,unsigned char以便可以为这两种字符类型使用适当的函数重载。

例如

    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);

在我尝试使用字符串流之前,这一切都很好。

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

MSVC Visual C++ Express V10 不会编译这个:

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
    ]

有任何想法吗?

** 2012 年 6 月 19 日编辑 **

好的,我已经更接近于理解这一点,但不是如何解决它。

众所周知,静态类变量被定义两次:

  • 一次在类定义和
  • 一旦在建立存储空间的类定义之外。

例如

    // 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;

它们受#ifs 保护,因此仅在构建 DLL 时才包含它们。否则它们将被排除在外。

numpunctchar和版本在 dll 中wchar_t

所以我们有类定义说 id 的存储在 DLL 中,但这仅适用于charwchar_t专业化,这意味着我的 unsigned char 版本注定要失败。:-(

我能想到的唯一方法是创建自己的专业:基本上是从头文件中复制并修复它。这引发了许多问题。

有人有更好的主意吗?

4

0 回答 0