(非扩展)操纵器通常只设置提取器随后读取并做出反应的标志和数据。(这就是xalloc
,iword
和pword
的用途。)显然,您可以做的是编写类似于 的内容std::get_money
:
struct uppercasify {
uppercasify(std::string &s) : ref(s) {}
uppercasify(const uppercasify &other) : ref(other.ref) {}
std::string &ref;
}
std::istream &operator>>(std::istream &is, uppercasify uc) { // or &&uc in C++11
is >> uc.ref;
boost::to_upper(uc.ref);
return is;
}
cin >> setw(80) >> uppercasify(mystring);
或者,cin >> uppercase
可以返回的不是对 的引用cin
,而是某个(模板)包装类的实例化uppercase_istream
,并带有对应的重载operator>>
。我不认为让操纵器修改底层流缓冲区的内容是一个好主意。
If you're desperate enough, I guess you could also imbue
a hand-crafted locale resulting in uppercasing strings. I don't think I'd let anything like that go through a code review, though – it's simply just waiting to surprise and bite the next person working on the code.