2015-02-06 31 views
1

我寫我自己的std::ctype<char16_t>每個虛擬成員函數的專業化,讓這個現在工作:GCC,的std :: CTYPE專業化和流

#include <string> 
#include <locale> 
#include "char16_facets.h" // Header containing my ctype specialisation 
#include <sstream> 
#include <iostream> 

// Implemented elsewhere using iconv 
std::string Convert(std::basic_string<char16_t>); 

int main() { 
    std::basic_string<char16_t> s("Hello, world."); 
    std::basic_stringstream<char16_t> ss(s); 
    ss.imbue(std::locale(ss.getloc(), new std::ctype<char16_t>())); 
    std::basic_string<char16_t> t; 
    ss >> t; 
    std::cout << Convert(t) << " "; 
    ss >> t; 
    std::cout << Convert(t) << std::endl; 
} 

有沒有一種方法,使數據流使用新ctype默認情況下,所以我不需要imbue每個流與新的區域設置?

我沒有寫一個新的類,只需提供

template<> 
inline bool std::ctype<char16_t>::do_is (std::ctype_base::mask m, char16_t c) const { 

等我會有點希望它會被自動拾取,只要它被宣佈之前,我#include <sstream>但它不是」噸。

上面的大部分工作都是使用G ++和libstdC++ 4.8完成的,但是我從SVN trunk獲得了相同的結果。

編輯 - 更新這個問題最初問到如何獲取號碼提取工作。但是,如果灌注了正確的ctypenumpunct實施方案,則不需要專門化num_get;只需

ss.imbue(std::locale(ss.getloc(), new std::num_get<char16_t>())); 

它會工作,無論是gcc版本。

再一次,有沒有辦法讓流自動選擇它,而不是必須使用它灌注每個流?

+0

'性病::區域::全球(標準::區域設置(),新的std :: CTYPE ());' – 0x499602D2 2015-02-06 14:08:09

+0

正是這樣 - 謝謝。還有一個問題 - 這是否取代了全球語言環境現有的ctype ?或者這是否意味着全局語言環境將同時具有ctype方面並將使用適合於流類型的方法?我在猜測後者,因爲它有效,但我無法在任何地方找到它。 – Tom 2015-02-12 00:00:01

+0

它取代了全局語言環境中的'std :: ctype'構面。我無法解釋它爲什麼如你所描述的那樣工作。 – 0x499602D2 2015-02-12 02:12:49

回答

3

使用std::locale::global()

std::locale::global(std::locale(std::locale(), new std::ctype<char16_t>())); 
+0

這樣比較好。答案去_answers_! – 2015-02-15 02:46:23