'R'字符串文本的上下文中是什么意思?

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

本文关键字:是什么 意思 文本 字符串 上下文      更新时间:2023-10-16

这段代码基本上与 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;
}

前缀(可选( R "delimiter( raw_characters )delimiter" (6( (自 C++11 起(

原始字符串文本。用于避免转义任何字符。分隔符之间的任何内容都将成为字符串的一部分。前缀(如果存在(与上述含义相同。

例:

const char* s1 = R"foo(
Hello
World
)foo";
//same as
const char* s2 = "nHellonWorldn";

其中foo是分隔符。


在您的情况下,message将打印:

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