GCC, std::ctype specialisation & streams

GCC, std::ctype specialisation & streams

本文关键字:streams specialisation ctype std GCC      更新时间:2023-10-16

我已经为std::ctype<char16_t>的每个虚拟成员函数编写了自己的专业化,因此现在可以使用:

#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主干构建的它们得到了相同的结果。

编辑-更新这个问题最初是关于如何进行数字提取的。然而,给定一个充满正确ctypenumpunct实现的流,则不需要对num_get进行专门化;简单

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

它将工作,与任何一个gcc版本。

同样,有没有什么方法可以让流自动收集这些信息,而不是让每条流都充满这些信息?

使用std::locale::global():

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