Topic: Defect Report: inconsistent example for new-line in a raw string literal
Author: "Alexander Antonov" <antalevlad@mtu-net.ru>
Date: Sat, 17 Jan 2009 23:34:03 CST Raw View
The document N2800 ("Document") contains folowing note
in the section 2.13.4 String literals [lex.string], paragraph 3 on
p.27:
[ Note: A source-file new-line in a raw string literal
results in a new-line in the resulting execution string-literal,
unless preceded by a backslash.
Assuming no whitespace at the beginning of lines
in the following example, the assert will succeed:
const char *p = R"[a
b
c]";
assert(std::strcmp(p, "ab\nc") == 0);
-end note ]
The statement "the assert will succeed" is ambiguous,
because the Document does not specify what the word "succeed" means
when applied to the functions std::strcmp() and assert()
that are defined in the separate document: C language standard.
The C language standard does not use the term "assertion succeed".
However, there is the term "assertion failed" that seems applicable
to example above.
The literal p contains two new-lines: after "a" and after "b",
and there are no backslashes.
So the one-line literal equivalent to p is "a\nb\nc".
Therefore, strcmp() will return non-zero,
and the comparison with 0 will evaluate to false.
Being passed as the argument to assert(), false compares equal to 0,
so assert() does output the message "Assertion failed" and calls
abort().
In turn, abort() generally causes abnormal program termination to
occur.
Details and special cases of abort() are specified in the C language
standard.
Formally, in order to return the example to the consistent state, it
is enough
to use "assert will call abort()" instead of "assert will succeed",
or simply add to literal p the backslash after character "a".
However, in this case the example still is based on C technology,
and the reader still has to know details of abnormal program
termination,
signal processing, and temporary files removal
to understand what assert() does (or hopes to avoid).
The reader also has to know details of C strings comparison
to understand what and when strcmp() returns.
All those C technology details cause an excessive
information searching work, although they are not necessary
for a demonstration of two interchangeable forms of the string
literal.
It is proposed to make folowing change in the Document:
in the section [lex.string], replace
[quote
the assert will succeed:
const char *p = R"[a
b
c]";
assert(std::strcmp(p, "ab\nc") == 0);
end quote]
with
[quote
const char *p = R"[a\
b
c]";
is the same as
const char *p ="ab\nc";
end quote]
The proposed example states equivalence of two forms
of the string literal explicitly
and demonstrates the use of a backslash.
Alexander Antonov
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]