std::codecvt::d o_in 方法重载与其他基本方法
std::codecvt::do_in method overloading vs the rest of base methods
我重载了do_in
std::codecvt
的方法:
#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;
}
并以此方式使用:
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
没有调用过载。我是否正确超载了它?我必须更改哪种std::codecvt<char, char, std::mbstate_t>
方法(以及如何(才能使我的分面调用do_in
方法?
我认为应该解决的第一件事是std::codecvt
系列方面仅由std::basic_filebuf
使用,因为仅在处理外部设备时才需要代码转换。在您的代码中,您将语言环境注入std::cin
中,该缓冲区不执行代码转换。
在程序中执行代码转换,但是阻止代码工作的方面是它继承自无法进行转换的std::codecvt<>
的专业化。std::codecvt<>
的char => char
专用化未定义转换,因此不会调用do_in()
因为不需要在这两种类型之间进行转换。
我尝试运行您的代码,但将继承的方面更改为std::codecvt<wchar_t, char, std::mbstate>
并使用宽字符文件流并且它有效。
如果您希望这也适用于窄字符流,我建议您创建一个流缓冲区,通过 underflow()
转发大写字符。
相关文章:
- C++:涉及继承和"using"时的方法重载
- 如何让编译器在C++中更喜欢常量方法重载?
- 为什么方法重载或枚举标志定义会触发 gcc7.2 编译器警告?
- C++模板方法重载和具有多态性的类访问
- 基于方法重载类
- 方法重载 C++ -> 只有一个方法调用
- 如何使用 SFINAE 禁用方法重载集?
- 使用类别方法重载 NSDecimalNumber 上的算术运算符
- C++继承和方法重载混淆
- 多态性和方法重载在C++中几乎是一样的
- 了解方法重载规则
- c++编译错误涉及方法重载的2个模板函数
- Qt/Qml 和方法重载
- 参数或方法重载的幻数
- 为什么C++更喜欢这个模板方法而不是方法重载
- C++方法重载
- 在C++中使用非友元方法重载 I/O 运算符 |&istream 和 &ostream 函数
- 插入方法重载
- 函数重载和方法重载之间的区别
- 正在解决跨基类的虚拟方法重载