问题
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& 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