为什么必须在C ++中转义字符串文字中的反斜杠? [英] Why must backslashes in string literals be escaped in C++?
问题描述
我想为两种语言声明相同的正则表达式模式。对于TCL我这样做
设置模式\d\s\S
但对于C ++,我必须为同一模式执行此操作。
boost :: regex pattern(\\d\\\\\\ s);
否则C ++编译器会告诉我们:
警告C4129:'d':无法识别的字符转义序列
所以为什么TCL不试图找到\d \s \S转义符号,只是忽略 \
-s但是C ++尝试和吸引? p>
PS
这就是C ++和PHP的区别;在PHP中,反斜杠后面的字符与一小组特殊字符(我相信rnvtx
)匹配。如果匹配失败,它只是继续,而不改变意思。
但是,C ++期望字符在这个小集合(我认为集合是更大的btw),但如果匹配失败,您将看到错误。
I want to declare the same regex pattern for both languages. For TCL I do this
set pattern "\d\s\S"
but for C++ I have to do this for the same pattern
boost::regex pattern("\\d\\s\\S");
otherwise C++ compiler will tell us the following:
warning C4129: 'd' : unrecognized character escape sequence
so why TCL don't try to find \d \s \S escape symbols and just ignores \
-s but C++ tries and sucks?
P.S. PHP works as TCL as I remeber.
This is just how C++ and PHP differ; in PHP, the character following a backslash is matched against a small set of special characters (I believe "rnvtx"
). If the match fails it will just continue without altering the meaning.
However, C++ expects the character to be in that small set (I think the set is bigger btw) but if the match fails you will see an error instead.
这篇关于为什么必须在C ++中转义字符串文字中的反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!