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

久未见 提交于 2019-12-02 10:43:06

问题


This code basically talks to a AMPS server and tries to publish a topic.

What is meaning of R in the second parameter of the 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:


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

Raw string literal. Used to avoid escaping of any character. Anything between the delimiters becomes part of the string. prefix, if present, has the same meaning as described above.

Example:

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

where foo is the delimiter.


In your case, a message would print:

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


来源:https://stackoverflow.com/questions/49365787/what-does-r-mean-in-the-context-of-string-literals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!