Online Book Reader

Home Category

Professional C__ - Marc Gregoire [250]

By Root 1558 0
1. If you write the following with a normal string literal, you will get a compiler error, because a normal string literal cannot span multiple lines and the example contains non-escaped quotes in the middle of the string, which are also not allowed:

string str = "Line 1

line "2" \t (and)

end";

To make the preceding work you need to use a raw string literal, which has the following general format:

R"d-char-sequence(r-char-sequence)d-char-sequence"

The d-char-sequence is an optional delimiter sequence, which should be the same at the beginning and at the end of the raw string. This delimiter sequence can have at most 16 characters. The r-char-sequence is the actual raw string. The preceding example can be modified to use a raw string literal as follows:

string str = R"~(Line 1

line "2" \t (and)

end)~";

Code snippet from RawStringLiteral\RawStringLiteral.cpp

In this example, the delimiter sequence is a single ~ character, which means the raw string literal has to start with R"~( and end with )~". As mentioned before, the delimiter is optional, so the following code is equivalent:

string str = R"(Line 1

line "2" \t (and)

end)";

Code snippet from RawStringLiteral\RawStringLiteral.cpp

If you write str to the console the output will be:

Line 1

line "2" \t (and)

end

With the raw string literal you do not need to escape quotes in the middle of the string and the \t escape character is not replaced with an actual tab character but is taken literally.

You might wonder what the point is of the optional delimiter sequence. It is required for strings that have a character sequence in the middle of the string that could be interpreted as the end of the raw string. For example, the following string is not valid because it contains the )" in the middle of the string, which is interpreted by the compiler as the end of the string:

string str = R"(The characters )" are embedded in this string)";

If you want the preceding string, you need to use a unique delimiter character, for example:

string str = R"-(The characters )" are embedded in this string)-";

Raw string literals will make life much easier for working with database querying strings, regular expressions, and so on. Regular expressions are discussed later in this chapter.

LOCALIZATION


When you’re learning how to program in C or C++, it’s useful to think of a character as equivalent to a byte and to treat all characters as members of the ASCII character set (American Standard Code for Information Interchange). ASCII is a 7-bit set usually stored in an 8-bit char type. In reality, experienced C++ programmers recognize that successful programs are used throughout the world. Even if you don’t initially write your program with international audiences in mind, you shouldn’t prevent yourself from localizing, or making the software local aware, at a later date.

Localizing String Literals

A critical aspect of localization is that you should never put any native-language literal strings in your source code, except maybe for debug strings targeted at the developer. In Microsoft Windows applications, this is accomplished by putting the strings in STRINGTABLE resources. Most other platforms offer similar capabilities. If you need to translate your application to another language, translating those resources should be all that needs to be done, without requiring any source changes. There are tools available that help you with this translation process.

To make your source code localizable, you should not use cout to compose sentences out of string literals, even if the individual literals can be localized. For example:

cout << "Read " << n << " bytes" << endl;

This cout statement cannot be localized to Dutch because it requires a reordering of the words. The Dutch translation is as follows:

cout << n << " bytes gelezen" << endl;

To make sure you can properly localize this cout statement, you could implement something as follows:

cout << Format(IDS_TRANSFERRED, n) << endl;

IDS_TRANSFERRED is the name of an entry in a string resource table. For the English

Return Main Page Previous Page Next Page

®Online Book Reader