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

时间:2018-03-19 14:49:42

标签: c++ string-literals

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

R的第二个参数中publish(的含义是什么?

#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;
}

1 个答案:

答案 0 :(得分:3)

  

前缀(可选)statics(6)(自C ++ 11起)

Raw string literal。用于避免任何角色的转义。分隔符之间的任何内容都成为字符串的一部分。 前缀(如果存在)与上述含义相同。

示例:

R "delimiter( raw_characters )delimiter"

其中const char* s1 = R"foo( Hello World )foo"; //same as const char* s2 = "\nHello\nWorld\n"; 是分隔符。

在您的情况下,foo会打印:

message