剛同事跑來問我 wstring 的問題,一看,哈!碰過了。問題是這樣子的,在 GCC 2.9x 上想要使用 wstring 卻無法使用,compile 時會出 error。原因有二:

第一個原因是,至少在 FreeBSD 4 以及我同事用的某一版 Linux 上,GCC 2.9x 沒有為我們預先 typedef std::basic_string<wchar_t>std::wstring,因此無 wstring 可用。不過,我們可以自行 typedef 如下以解決這個問題:

#if (defined(__FreeBSD__) && defined(__GNUC__) && (__GNUC__ == 2))
#   include <string>
    namespace std {
        typedef std::basic_string<wchar_t> wstring;
    } // namespace std
#endif

我們可以把上面這段程式碼,放在某個 header 檔裡,有需要時便引入以解決這個問題。像我便是放在一個專門處理 platform portability 問題的 header 檔。

第二個原因就真的是 GCC 2.9x 的 bug 了。這個 bug 的 report 與 fix 可以在下面這個網頁找到:《libstdc++/95: bastring.h's c_str implementation prohibits charT's other than char》。

基本上就是,GCC 2.9x 在 std::basic_stringc_str() 實作裡,用到了 "" 這個 string literal,因為其 type 為 const char*,當 std::basic_string 的 template parameter charT 不是 char 的時候,就會造成 compilation error。解法就是照著上述連結,去修正 GCC 的 std/bastring.h 這個檔便可以了。 要寫 i18n 程式,狗屁倒灶的事還多著呢,有空再來慢慢聊。