When using multi-line raw string literals
, how do we tell the compiler not to include the leading spaces?
I have the following bit of code:
#include <string>
#include <print>
int main()
{
constexpr auto preamble =
R"(
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta name="Author" content="Some One" />
<title>Title Of Page</title>
</head>
<body style="">
)";
std::println("{}", preamble);
return 0;
}
The output is as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta name="Author" content="Some One" />
<title>Title Of Page</title>
</head>
<body style="">
Which seems to take the leading spaces into account. I'm looking for something like the following:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta name="Author" content="Some One" />
<title>Title Of Page</title>
</head>
<body style="">
I know I can shift the definition all the way to the left, but when the string-literal is within a class method or within nested namespaces, it sort of breaks the indentation of the code.
A raw string literal is always as "raw" as possible in the sense that it will hold exactly the code points present in the source file, with the minor exception that \r
and \r\n
in the source file are replaced by \n
.
If you do want the whitespace in the source, but not the literal, you can't have the literal span the whitespace.
One way to avoid this is to put each line in its own literal, adding the newline manually:
constexpr auto preamble =
R"(<?xml version="1.0" encoding="iso-8859-1"?>)" "\n"
R"(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">)" "\n"
R"(<html xmlns="https://www.w3.org/1999/xhtml">)" "\n"
R"(<head>)" "\n"
R"( <meta name="Author" content="Some One" />)" "\n"
R"( <title>Title Of Page</title>)" "\n"
R"(</head>)" "\n"
R"(<body style="">)" "\n"
;
Another solution would be to store the string literal with the whitespace into a std::string
and then manually remove the whitespace at runtime (or compile-time even if you store the result in a std::array
).