'R' 在字符串文字的上下文中意味着什么?

What does 'R' mean in the context of string literals?

此代码基本上与 AMPS 服务器对话并尝试发布主题。

publish()的第二个参数R是什么意思?

#include <ampsplusplus.hpp>
#include <iostream>

int main(void)
{
    const char* uri = "tcp://127.0.0.1:9007/amps/json";

    // Construct a client with the name "examplePublisher".

    AMPS::Client ampsClient("examplePublisher");

    try
    {
        // connect to the server and log on
        ampsClient.connect(uri);
        ampsClient.logon();

        // publish a JSON message
        ampsClient.publish("messages",
                           R"({ "message" : "Hello, World!" ,)"
                           R"(client" : 1 })");

    }
    catch (const AMPS::AMPSException&amp; e)
    {
        std::cerr << e.what() << std::endl;
        exit(1);
    }
    return 0;
}

prefix(optional) R "delimiter( raw_characters )delimiter" (6) (since C++11)

Raw string literal。用于避免转义任何字符。定界符之间的任何内容都成为字符串的一部分。 prefix,如果存在,含义与上述相同。

示例:

const char* s1 = R"foo(
Hello
World
)foo";
//same as
const char* s2 = "\nHello\nWorld\n";

其中 foo 是分隔符。


在你的例子中,message 会打印:

{ "message" : "Hello, World!" ,client" : 1 }