11

According to the C++ standard (ยง30.7.5.2.4 of C++17 draft (N4659)), out << ch will not perform a widening operation on ch, if ch is a char and out is a std::ostream.

Does this imply that std::ctype<char>::widen() (i.e., char -> char) is guaranteed by the standard to be an identity function (widen(ch) == ch) for all characters in the basic source character set?

If so, does this, in turn, imply that all locales are required by the standard to use the same non-wide (or multi-byte) encoding of characters from the basic source character set?

If not, it seems like out << 'x', with a particular choice of character encoding for literals, might not work in all locales, even when it works in some. That is, there might be no choice of character literal encoding, such that out << 'x' works in all locales simultaneously.

Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43

1 Answers1

1

No, it just says that in the case of

template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, char c);

where both the stream and the << operator trades in the same char type there is no conversion.

If c has type char and the character type of the stream is not char, then seq consists of out.widen(c); otherwise seq consists of c.

In all other cases the locale is used to optionally transform the character with no restrictions on what the locales might do.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Ok, yes, but what am I supposed to learn from that? The fact that an invocation of widen may not be elided in the case of strings, seems to suggest that there are valid cases where the widening operation does something. But if so, how can it be alright that the widening operation is bypassed when outputting a single character? โ€“ Kristian Spangsege Jun 11 '20 at 02:41
  • I guess my question is this: If I was to implement an integer formatting function, for instance, how am I supposed to think about the difference in roles (domains of applicability) between the two output operations (single character vs string)? Naively, one would expect that outputting two chars separately would have the same effect as outputting a single string with those two chars. โ€“ Kristian Spangsege Jun 11 '20 at 02:47
  • But it seems like there must be some intended difference in roles. I'm trying to understand what that is. โ€“ Kristian Spangsege Jun 11 '20 at 02:48