Skip to content
Search

C++:literal

lastmod 2020-09-01created 2016-05-0312 edits

Invariant program elements are called "literals" or "constants." The terms "literal" and "constant" are used interchangeably here. Literals fall into four major categories: integer, character, floating-point, and string literals.

Literals

A literal may be any of the following:

  • integer-constant
  • character-constant
  • floating-constant
  • string-literal

About

Notes on terminology

In C++, an unprefixed string literal token is called an ordinary string literal (Ordinary string literal).

"Ordinary string literal" and "UTF-8 string literals" are both classified as Narrow string literals because their element type is char.

The rest, left untranslated:

However, the term "narrow string literal" is actually used only three times in two paragraphs of the C++ WD.

In C, an unprefixed string literal is called a "character string literal". In the status quo, every prefixed string literal is (some kind of) wide string literal. The term "character string literal" is used in several places, especially in the preprocessor description, where it is clear that an unprefixed string literal is exactly what is meant. So the term "UTF-8 string literal" is introduced to refer to a prefixed string literal that is nevertheless not a wide string literal.

WG14 has already rejected a proposal to drop the term "character string literal" in favor of some other term (and WG21 has rejected the term "character string literal" for an unprefixed string literal).

The amended grammar for string-literal exactly parallels that in C++, omitting only the alternative for raw strings. The new non-terminal encoding-prefix is exactly as in C++. This new term considerably simplifies the discussion of string token concatenation.

Syntax

int n8 = 0377; // 8진수. 0으로 시작한다.
int n10 = 212; // 10진수.
int n16 = 0x377; // 16진수. 0x 또는 0X로 시작한다.

Examples

157 // integer constant
0xFE // integer constant
'c' // character constant
0.2 // floating constant
0.2E-01 // floating constant
"dog" // string literal

C++11 Literals

The literals available since C++11 are as follows.

int n2 = 0b101; // 2진수. 0b 또는 0B로 시작한다.

RAW String

No escape characters are used. It must be wrapped in R"somthing( and )somthing".

char * raw_str = R"(str\str)"; // RAW 문자열. regex등을 사용할 경우 유용하다.

Character encoding

char * string = u8"가나다";

Template based char or wchar_t literal

Like the _T macro used with TCHAR in the WindowsAPI, a template-based way to switch literals is as follows.

template<typename C>
const C * ChooseCW(const char * c, const wchar_t * w);

template<>
const char * ChooseCW<char>(const char * c, const wchar_t * w)
{
    return c;
}

template<>
const wchar_t * ChooseCW<wchar_t>(const char * c, const wchar_t * w)
{
    return w;
}

#define CW(C, STR) ChooseCW<C>(STR, L##STR)

insert(value_type(CW(C, "in1"), CW(C, "out1")));

Troubleshooting

string too big, trailing characters truncated

The following warning may occur in MSVC.

string too big, trailing characters truncated

Keep the maximum length of the string literal at 16K or less.

VC++ 6, VC++ 2005, VC++ 2008
The maximum length of a string literal is approximately 2,048 bytes.
VC++ 2010, VC++ 2012
The maximum length of a string literal is 16,384 bytes (16K).

See also

Favorite site

author Zer0 sitemap