DEV Community

Swastik Baranwal
Swastik Baranwal

Posted on • Updated on

Modern C++ : Raw Strings

Following my series from less known features of Modern C++.

From C++11, you now have raw strings which is much needed when writing strings of special characters or escape sequences like \n, \r, \ etc.

When this problem arises

In the following code we have two strings which represent a path and a special string respectively. We need the output to be C:\User\Home\Folder and newline\n.

#include <iostream>
#include <string>

int main() {

std::string dir = "C:\User\Home\Folder";  
std::string special_str = "newline\n"; 

std::cout << dir << std::endl;
std::cout << special_str << std::endl;

 return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

main.cpp: In function ‘int main()’:
main.cpp:15:19: error: incomplete universal character name \U
 std::string dir = "\User\Home\Folder";
                   ^~~~~~~~~~~~~~~~~~~
main.cpp:15:19: warning: unknown escape sequence: '\H'
main.cpp:15:19: warning: unknown escape sequence: '\F'
Enter fullscreen mode Exit fullscreen mode

This is something that we didn't expected to happen. Thus, a problem like this comes when we need string with special characters.

Possible Solutions

Using \\in strings

In Traditional C++, you had to do this to solve this problem:

#include <iostream>
#include <string>

int main() {

std::string dir = "C:\\User\\Home\\Folder";
std::string special_str = "newline\\n";

std::cout << dir << std::endl;
std::cout << special_str << std::endl;

 return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

C:\User\Home\Folder                                                                                                                                   
newline\n
Enter fullscreen mode Exit fullscreen mode

In the above we had to do \ twice to make the compiler ignore the escape sequence but this looks ugly IMO.

Using R for raw string

From C++11 on-wards, we can write the code more easily and fancy with the help of R in-front of the string and parenthesis in the string which treats special characters as literal.

#include <iostream>
#include <string>

int main() {

std::string dir = R"(C:\User\Home\Folder)";
std::string special_str = R"(newline\n)";

std::cout << dir << std::endl;
std::cout << special_str << std::endl;

 return 0;
}                                                                                                                                 
Enter fullscreen mode Exit fullscreen mode

Output:

C:\User\Home\Folder                                                                                                                                   
newline\n
Enter fullscreen mode Exit fullscreen mode

In the above code we have used R"()" to treat strings with special characters as literals which makes the code compile and looks fancy, easy and debug.

I will say that people who use Modern C++ must go with the second method because it's modern and many people will learn it when they are reading code but the first method should also be known for learning purposes.

I hope this clarifies about raw strings in Modern C++ and if you have any question then please don't hesitate to ask!

Acknowledgments

I thank Modern C++ Book, C++ Preference for providing the more detailed information. I wouldn't have been able to make this post if these resources weren't present.

Top comments (6)

Collapse
 
danielvandenberg95 profile image
Daniël van den Berg

It's usually C:\, not C\ 😅

Collapse
 
delta456 profile image
Swastik Baranwal

Fixed!

Collapse
 
jmccabe profile image
John McCabe

I'm not convinced by your "Looks ugly" / "Looks much better" choices; I don't think either looks great!

Collapse
 
delta456 profile image
Swastik Baranwal

I think you are right but I will strongly prefer people on going with the second method though.

Collapse
 
jmccabe profile image
John McCabe

Personally, I'd much prefer the first option. It's what's been used for decades and, although it's a bit ugly (especially if you use SMB shares that already involve double backslashes), I just think the R"(xxx)" notation is pretty awful. In Python it's r'string', no parentheses required, so this C++ version looks like an abomination that's hasn't been thought about properly to me.

Thread Thread
 
delta456 profile image
Swastik Baranwal

I just want people to know these things because it looks bad when seeing Traditional things but I also didn't like () too.

So yeah you are right!