Skip to content

C++:literal

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

전문 용어에 대한 참고 사항

C++ 에서 접두사가없는 문자열 리터럴 토큰을 일반 문자열 리터럴 (Ordinary string literal)이라고 한다.

"Ordinary string literal"과 "UTF-8 string literals"은 모두 Element type이 char이기 때문에 Narrow string literals로 분류된다.

번역되지 않은, 이후 내용:

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

C++11 부터 사용가능한 리터럴은 아래와 같다.

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

RAW String

Escape character를 사용하지 않는다. R"somthing()somthing"로 묶어야 한다.

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

Character encoding

char * string = u8"가나다";

Template based char or wchar_t literal

WindowsAPI의 TCHAR에서 사용하는 _T매크로와 같이, 템플릿 기반의 리터럴 변경방법은 아래와 같다.

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

MSVC에서 아래와 같은 경고가 발생될 수 있다.

string too big, trailing characters truncated

문자열 리터럴의 최대 길이를 16K 이하로 조절하면 된다.

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