C ++什么时候在输出流运算符<<()中加宽字符? [英] C++ When are characters widened in output stream operator<<()?

查看:49
本文介绍了C ++什么时候在输出流运算符<<()中加宽字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,C ++标准(尤其是C ++ 17草案(N4659)的§30.7.5.2.4)中存在一个不一致的地方,关于何时在输出流上格式化输出操作中的字符变宽了( operator<<()). en.cppreference.com 似乎完全反映了相同的不一致之处.

It seems to me, that there is an inconsistency in the C++ standard, specifically in §30.7.5.2.4 of the C++17 draft (N4659), about when characters are widened in formatted output operations on output streams (operator<<()). Exactly the same inconsistency seems to be reflected in en.cppreference.com.

首先,假设以下声明:

std::ostream out;
std::wostream wout;
char ch;
wchar_t wch;
const char* str;
const wchar_t* wstr;

然后指出

  1. out<<ch 执行字符加宽,
  2. out<<str 执行字符加宽,
  3. wout<<ch 执行字符加宽,
  4. wout<<str 执行字符加宽,
  5. wout<<wch 执行字符加宽,
  6. wout<<wstr 执行字符加宽.
  1. out << ch does not perform character widening,
  2. out << str performs character widening,
  3. wout << ch performs character widening,
  4. wout << str performs character widening,
  5. wout << wch does not perform character widening,
  6. wout << wstr performs character widening.

第一个也是最明显的不一致之处是(6)不能为真,因为没有 widen()函数带有 wchar_t 参数,只有一个带有 char 参数.

The first and most obvious inconsistency is that (6) cannot be true, as there is no widen() function taking a wchar_t argument, only one that takes a char argument.

第二个(似乎)不一致是在(1)和(2)之间. out<<对我来说似乎很奇怪."x" 应该加宽'x',而 out<<'x'不应该.

The second (seeming) inconsistency is between (1) and (2). It seems strange to me that out << "x" should widen 'x', while out << 'x' should not.

我误解了标准文本,还是那里出了点问题?如果后者是正确的,那么您知道预期的行为是什么吗?

Am I misinterpreting the standard text, or is there something wrong there? If the latter is true, do you know what the intended behavior is?

显然,自从至少C ++ 03(第27.6.2.5.4节)以来,这种不一致(如果我是对的话)就已经存在于标准中.文本在中间标准中有所变化,但是仍然存在不一致的地方,如我上面解释的那样.

Apparently, this inconsistency (if I am right), has been present in the standard since at least C++03 (§27.6.2.5.4). The text changes a bit through the intermediate standards, but the inconsistency, as I explain it above, remains.

推荐答案

该标准似乎并不完全正确.大多数问题源于各个操作的批量规格.而不是单独处理每个重载,而是将相似的重载一起描述,从而导致产生误导性的规范.

It looks as if the standard isn't entirely correct. Most of the issue stems from the bulk-specification of the respective operations. Instead of handling each overload individually similar overloads are described together resulting in a misleading specification.

我怀疑,但是任何实现者都难以理解其意图.本质上,当将 char 插入非 char 流中时,需要对字符进行 widen()以获得流中字符的字符类型.这种扩展旨在将源字符集中的一个字符映射到流的宽字符集中的一个字符.

I doubt, any implementer has any trouble understanding what is intended, though. Essentially when a char is inserted into a non-char stream the character needs to be widen()ed to obtain the character of the stream's character type. This widening is intended to map one character from the source character set to the one character in the stream's wide character set.

请注意,IOStreams规范假定流中字符的原始概念是单个实体.自从创建了规范(针对C ++ 1998版本)以来,文本并未真正进行实质性的更新,但是由于Unicode的广泛使用,流中的字符"实际上是编码的字节.尽管在此修改后的环境中,流在大多数情况下都可以正常运行,但实际上并没有正确支持某些灵活性,这些灵活性可能有助于处理Unicode字符.缺少将一个字符扩展"为UTF8字节序列的内容可能就是其中之一.

Note that the IOStreams specification assumes the original notion of characters in streams being individual entities. Since the specification was created (for the C++1998 version) the text wasn't really updates substantially but with wide use of Unicode the "characters" in a stream are really bytes of an encoding. Although the streams mostly function OK in this modified environment, some flexibility which would be helpful to deal with Unicode characters isn't really properly supported. The absence of something "widening" one character into a sequence of UTF8 bytes is probably one of these.

如果您认为流部分中的不一致/不正确之处需要解决,请提交缺陷报告.提交缺陷报告的说明位于 http://isocpp.org .当您确实提出问题时,请考虑提供建议的措辞以更正此问题.由于不存在不清楚的含义,实际上大多数实现可能都会做正确的事情,因此我希望这个问题的优先级较低,并且如果没有建议的措辞,就不太可能引起太大关注.当然,解决该问题不会改变预期的行为,例如,将 char s扩展为UTF8序列:这实际上是对流库的重新设计,虽然顺序合理,但不会作为缺陷解决方案的一部分来完成.

If you feel the inconsistency/incorrectness in the stream's section warrants addressing, file a defect report. Instruction on filing defect reports are at http://isocpp.org. When you do raise an issue consider providing proposed wording to correct the issue. Since there is no lack of clarity what is actually intended and probably most implementations do the right thing anyway I'd expect this issue to get fairly low priority and without proposed wording it is unlikely to receive much attention. Of course, addressing the issue won't change the intended behavior, e.g., to "widen" chars into a UTF8 sequence: that would effectively be a redesign of the streams library which may be in order but won't be done as part of defect resolution.

这篇关于C ++什么时候在输出流运算符&lt;&lt;()中加宽字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆