简体   繁体   English

C ++避免反斜杠和双引号转义

[英]C++ Avoid backslash and double quotes from escaping

I want to know how to avoid backslash and double-quotes from escaping it's expression. 我想知道如何避免反斜杠和双引号转义它的表达式。 Would using R() work with LPCSTR? 使用R()可以与LPCSTR一起使用?

Backslash: 反斜杠:

WriteKey("\"); //this will escaped accidentally.
WriteKey("/"); //this wouldn't

Same as double-quotes: 与双引号相同:

WriteKey("""); //this would escape too
WriteKey("'"); //but not this

Note: WriteKey() would need LPCSTR for it's argument 注意: WriteKey()的参数需要LPCSTR

Would using R() work with LPCSTR? 使用R()可以与LPCSTR一起使用?

Yes. 是。 Raw string literals produce strings with the same types as their non-raw counterparts. 原始字符串文字会产生与非原始字符串相同类型的字符串。

The following lines pass strings with the same content to WriteKey . 以下几WriteKey内容相同的字符串传递给WriteKey

WriteKey("\\");   // manual escaping
WriteKey(R"(\)"); // raw string literal

As you can see, it doesn't make much sense to use raw string literals with very short strings. 如您所见,使用非常短的字符串使用原始字符串文字没有多大意义。

Raw string literals are supported in Visual Studio starting with VS2013. 从VS2013开始,Visual Studio支持原始字符串文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM