Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

escape sequence '\('

36 views
Skip to first unread message

Ralf Goertz

unread,
Jun 22, 2016, 9:47:34 AM6/22/16
to
What is the following warning given by g++ supposed to mean?

es_test.cc:6:32: warning: unknown escape sequence: '\)'
std::string legal="\(",illegal="\)";
^
In particular, if '\(' is a known escape sequence (for what?) why isn't
'\)'?

Christian Gollwitzer

unread,
Jun 22, 2016, 10:10:46 AM6/22/16
to
Am 22.06.16 um 15:47 schrieb Ralf Goertz:
> What is the following warning given by g++ supposed to mean?
>
> es_test.cc:6:32: warning: unknown escape sequence: '\)'
> std::string legal="\(",illegal="\)";
> ^

In general, you should double all literal backslashes in a string, in
order not to fall for something like "C:\newstuff" which gets
interpreted as a newline character. The backslash is treated as a
literal backslash if the character that follows does not give a vald
escape sequence; but even if it des not now, a future compiler miht
treat it differently.

> In particular, if '\(' is a known escape sequence (for what?) why isn't
> '\)'?


I don't know, but since you intend to write literal backslashes, you
should write it as "\\(" and "\\)"

Christian

Ralf Goertz

unread,
Jun 22, 2016, 11:00:21 AM6/22/16
to
Am Wed, 22 Jun 2016 16:10:36 +0200
schrieb Christian Gollwitzer <auri...@gmx.de>:

> Am 22.06.16 um 15:47 schrieb Ralf Goertz:
>
> > In particular, if '\(' is a known escape sequence (for what?) why
> > isn't '\)'?
>
>
> I don't know, but since you intend to write literal backslashes, you
> should write it as "\\(" and "\\)"

No, I don't intend to write that. I came across that warning when I
accidentally omitted one backslash trying to write "\\\\)". I
experimented and discovered that "\(" didn't give the warning. Since I
had never seen the escape sequence '\(' I looked it up in the standard.
But there I only found: "Escape sequences in which the character
following the backslash is not listed in Table 7 are
conditionally-supported, with implementation-defined semantics." Table 7
doesn't list '\('. So I figured gcc might have defined it. But I found
nothing in the man pages. That's why I asked.

guinne...@gmail.com

unread,
Jun 22, 2016, 12:19:43 PM6/22/16
to
'\(' isn't a known escape sequence and, as such, should be just as
"illegal" as '\)'.

The question really should be "why does g++ accept this non-standard
escape sequence?". Which, of course, only the authors, documentation
and/or source code comments of g++ can tell you.

Out of interest, I've tested for other such undocumented escape sequences.
Apart from the "legal" ones (as defined by The Standard), g++ also
recognises '\%', '\(', '\[' and '\{', which all translate to their
unescaped versions. Additionally, it recognises '\E' and '\e',
translating both to ASCII <ESC>.

Richard

unread,
Jun 22, 2016, 1:42:09 PM6/22/16
to
[Please do not mail me a copy of your followup]

Christian Gollwitzer <auri...@gmx.de> spake the secret code
<nke68s$1bi$1...@dont-email.me> thusly:

>Am 22.06.16 um 15:47 schrieb Ralf Goertz:
>> What is the following warning given by g++ supposed to mean?
>>
>> es_test.cc:6:32: warning: unknown escape sequence: '\)'
>> std::string legal="\(",illegal="\)";
>> ^
>
>In general, you should double all literal backslashes in a string,

Good god, no.

Just use raw string literals.

C++11, ya know. It's already 5 years old.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Richard

unread,
Jun 22, 2016, 1:43:34 PM6/22/16
to
[Please do not mail me a copy of your followup]

Ralf Goertz <m...@myprovider.invalid> spake the secret code
<20160622170...@delli.home.local> thusly:

>No, I don't intend to write that. I came across that warning when I
>accidentally omitted one backslash trying to write "\\\\)".

VS 2015 can refactor such string literals to raw string literals and I
added a similar check to clang-tidy:
<http://clang.llvm.org/extra/clang-tidy/checks/modernize-raw-string-literal.html>

Victor Bazarov

unread,
Jun 22, 2016, 1:51:37 PM6/22/16
to
On 6/22/2016 1:41 PM, Richard wrote:
>[..]
> C++11, ya know. It's already 5 years old.

Can you trust a 5-year-old with your stuff? ;-)

V
--
I do not respond to top-posted replies, please don't ask

Ben Bacarisse

unread,
Jun 22, 2016, 3:08:06 PM6/22/16
to
guinne...@gmail.com writes:

> On Wednesday, 22 June 2016 14:47:34 UTC+1, Ralf Goertz wrote:
>> What is the following warning given by g++ supposed to mean?
>>
>> es_test.cc:6:32: warning: unknown escape sequence: '\)'
>> std::string legal="\(",illegal="\)";
<snip>
> Out of interest, I've tested for other such undocumented escape sequences.
> Apart from the "legal" ones (as defined by The Standard), g++ also
> recognises '\%', '\(', '\[' and '\{', which all translate to their
> unescaped versions. Additionally, it recognises '\E' and '\e',
> translating both to ASCII <ESC>.

My g++ version (5.2.1) rejects them all if asked to be conforming
(-std=c++11 -pedantic).

--
Ben.

Scott Lurndal

unread,
Jun 22, 2016, 3:40:25 PM6/22/16
to
legaliz...@mail.xmission.com (Richard) writes:
>[Please do not mail me a copy of your followup]
>
>Christian Gollwitzer <auri...@gmx.de> spake the secret code
><nke68s$1bi$1...@dont-email.me> thusly:
>
>>Am 22.06.16 um 15:47 schrieb Ralf Goertz:
>>> What is the following warning given by g++ supposed to mean?
>>>
>>> es_test.cc:6:32: warning: unknown escape sequence: '\)'
>>> std::string legal="\(",illegal="\)";
>>> ^
>>
>>In general, you should double all literal backslashes in a string,
>
>Good god, no.
>
>Just use raw string literals.
>
>C++11, ya know. It's already 5 years old.

But not an option for everyone, unfortunately.

Kalle Olavi Niemitalo

unread,
Jun 22, 2016, 8:09:51 PM6/22/16
to
guinne...@gmail.com writes:

> The question really should be "why does g++ accept this non-standard
> escape sequence?". Which, of course, only the authors, documentation
> and/or source code comments of g++ can tell you.

The C source code of GNU Emacs often uses \( to escape the
opening parenthesis when it occurs at the beginning of a line in
a multiline string literal. AFAIK, this helps Emacs understand
that the parenthesis does not mark the start of a function,
when the source code of Emacs is being edited with Emacs itself.
Because GCC and Emacs are flagship GNU packages, it seems
reasonable for GCC not to warn about Emacs conventions by
default.

https://www.gnu.org/prep/standards/html_node/Formatting.html
also advises against starting a line with an opening parenthesis.
It does not mention the backslashes though.
0 new messages