首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::codecvt::do_in方法重载与其他基本方法

std::codecvt::do_in方法重载与其他基本方法
EN

Stack Overflow用户
提问于 2014-03-27 13:21:30
回答 1查看 254关注 0票数 0

我已经重载了do_in方法的std::codecvt

代码语言:javascript
复制
#include <iostream>
#include <locale>
#include <string>

class codecvt_to_upper : public std::codecvt<char, char, std::mbstate_t> {
public:

    explicit codecvt_to_upper(size_t r = 0) : std::codecvt<char, char, 
                                                          std::mbstate_t>(r) {}
protected:
    result do_in(state_type& state, const extern_type* from,
                 const extern_type* from_end, const extern_type*& from_next,
                 intern_type* to, intern_type* to_end, intern_type*& to_next)
                   const;

    result
    do_out(state_type& __state, const intern_type* __from,
            const intern_type* __from_end, const intern_type*& __from_next,
            extern_type* __to, extern_type* __to_end,
            extern_type*& __to_next) const {
        return codecvt_to_upper::ok;
    }

    result
    do_unshift(state_type& __state, extern_type* __to,
            extern_type* __to_end, extern_type*& __to_next) const {
        return codecvt_to_upper::ok;
    }

    int
    do_encoding() const throw () {
        return 1;
    }

    bool
    do_always_noconv() const throw () {
        return false;
    }

    int
    do_length(state_type&, const extern_type* __from,
            const extern_type* __end, size_t __max) const {
        return 1;
    }

    int
    do_max_length() const throw () {
        return 10;
    }
};

codecvt_to_upper::result codecvt_to_upper::do_in(state_type& state, 
                  const extern_type* from, const extern_type* from_end, const 
                  extern_type*& from_next, intern_type* to, intern_type* 
                  to_end, intern_type*& to_next) const {
    codecvt_to_upper::result res = codecvt_to_upper::error;
    const std::ctype<char>& ct = std::use_facet<std::ctype<char> >( 
                                                               std::locale());

    const extern_type* p = from;
    while( p != from_end && to != to_end) {
        *to++ = ct.toupper( *p++);
    }
    from_next = p;
    to_next = to;
    res = codecvt_to_upper::ok;
    return res;
}

并以这种方式使用:

代码语言:javascript
复制
int main(int argc, char** argv) {

    std::locale ulocale( std::locale(), new codecvt_to_upper);

    std::cin.imbue( ulocale);

    char ch;
    while ( std::cin >> ch) {
        std::cout << ch;
    }
    return 0;
}

但是没有调用do_in重载。我是否正确地重载了它?的哪种方法(以及如何)必须更改以使我的方面调用do_in方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-27 17:04:22

我认为应该解决的第一件事是,std::codecvt族方面仅由std::basic_filebuf使用,因为只有在处理外部设备时才需要进行代码转换。在您的代码中,您将区域设置注入到std::cin中,后者具有一个不执行代码转换的缓冲区。

当然,在程序中执行代码转换仍然是可能的,但是阻碍您的代码工作的方面是,它继承自不能进行转换的std::codecvt<>的专门化。char => char专门化的std::codecvt<>不定义转换,因此不会调用do_in(),因为没有必要在这两种类型之间进行转换。

我试着运行您的代码,但是将继承的方面更改为std::codecvt<wchar_t, char, std::mbstate>,并使用宽字符文件流和啊,真灵

如果您希望这也适用于窄字符流,我建议创建一个流缓冲区,通过underflow()转发大写字符。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22688965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档