Why are backslashes in string literals avoided in C ++?

I want to declare the same regular expression pattern for both languages. For TCL, I do it

set pattern "\d\s\S" 

but for C ++ I have to do this for the same template

 boost::regex pattern("\\d\\s\\S"); 

otherwise the C ++ compiler will tell us the following:

 warning C4129: 'd' : unrecognized character escape sequence 

so why doesn't TCL try to find the escape characters \ d \ s \ S and just ignore \ -s, but does C ++ also suck?

PS PHP works like TCL, as I recall.

+4
source share
2 answers

This is exactly how C ++ and PHP differ; in PHP, the character following the backslash is mapped to a small set of special characters (I consider "rnvtx" ). If the match fails, it will continue without changing the value.

However, C ++ expects the character to be in this small set (I think the set is larger than btw), but if the match fails, you will see an error.

+4
source

C ++ has the concept of a character escape sequence . Escape sequences that take the form \c ("c" being a character) are used to define specific special characters in string literals, so it follows that backslashes themselves must also be escaped to indicate that the special character isn ' t is implied.

+1
source

All Articles