Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

c++ - what does cout << " "[a==N]; do?

In the following example:

cout<<"
"[a==N];

I have no clue about what the [] option does in cout, but it does not print a newline when the value of a is equal to N.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have no clue about what the [] option does in cout

This is actually not a cout option, what is happening is that " " is a string literal. A string literal has the type array of n const char, the [] is simply an index into an array of characters which in this case contains:



note is appended to all string literals.

The == operator results in either true or false, so the index will be:

  • 0 if false, if a does not equal N resulting in
  • 1 if true, if a equals N resulting in

This is rather cryptic and could have been replaced with a simple if.

For reference the C++14 standard(Lightness confirmed the draft matches the actual standard) with the closest draft being N3936 in section 2.14.5 String literals [lex.string] says (emphasis mine):

string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

and:

After any necessary concatenation, in translation phase 7 (2.2), ’’ is appended to every string literal so that programs that scan a string can find its end.

section 4.5 [conv.prom] says:

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

Writing a null character to a text stream

The claim was made that writing a null character() to a text stream is undefined behavior.

As far as I can tell this is a reasonable conclusion, cout is defined in terms of C stream, as we can see from 27.4.2 [narrow.stream.objects] which says:

The object cout controls output to a stream buffer associated with the object stdout, declared in <cstdio> (27.9.2).

and the C11 draft standard in section 7.21.2 Streams says:

[...]Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing characters and the control characters horizontal tab and new-line;

and printing characters are covered in 7.4 Character handling <ctype.h>:

[...]the term control character refers to a member of a locale-specific set of characters that are not printing characters.199) All letters and digits are printing characters.

with footnote 199 saying:

In an implementation that uses the seven-bit US ASCII character set, the printing characters are those whose values lie from 0x20 (space) through 0x7E (tilde); the control characters are those whose values lie from 0 (NUL) through 0x1F (US), and the character 0x7F (DEL).

and finally we can see that the result of sending a null character is not specified and we can see this is undefined behavior from section 4 Conformance which says:

[...]Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior.[...]

We can also look to the C99 rationale which says:

The set of characters required to be preserved in text stream I/O are those needed for writing C programs; the intent is that the Standard should permit a C translator to be written in a maximally portable fashion. Control characters such as backspace are not required for this purpose, so their handling in text streams is not mandated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...