According to Bjarne Stroustrup's Programming Principles and Practice Using C++ (Sixth Printing, November 2012), "each of [the] types [int
, double
, char
, string
, and bool
] has its own characteristic style of literals" (p.63).
In §A.2 of the same book, additional varieties of int
(unsigned; long) and floating-point-literals are mentioned, as well as the (null) pointer literal.
Does every type have a corresponding literal?
C++ Standard n3337 § 2.14.1/1:
There are several kinds of literals.
literal:
integer-literal
character-literal
floating-literal
string-literal
boolean-literal
pointer-literal
user-defined-literal
No, not avery type has a literal. Classes do not have literal, as well as i.e void
, int*
, signed char
, unsigned char
, short
, unsigned short
.
Literals explained:
integer-literal
An integer literal is a sequence of digits that has no period or exponent part.
example: 1
character-literal
A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by
one of the letters u, U, or L, as in u’y’, U’z’, or L’x’, respectively.
’ c-char-sequence ’
u’ c-char-sequence ’
U’ c-char-sequence ’
L’ c-char-sequence ’
example: 'a'
, 'ab'
, '\''
, L'0'
, '('
floating-literal
A floating literal consists of an integer part, a decimal point, a fraction part, an e or E, an optionally signed
integer exponent, and an optional type suffix. The integer and fraction parts both consist of a sequence of
decimal (base ten) digits. Either the integer part or the fraction part (not both) can be omitted; either the
decimal point or the letter e (or E ) and the exponent (not both) can be omitted. The integer part, the
optional decimal point and the optional fraction part form the significant part of the floating literal. The
exponent, if present, indicates the power of 10 by which the significant part is to be scaled.
example: 123.456e-67
, .1E1f
, 42.
, 13e3
string-literal
A string literal is a sequence of characters (as defined in 2.14.3) surrounded by double quotes, optionally
prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, as in "...", R"(...)", u8"...", u8R"(...)", u"...",
uR"* ̃(...)* ̃", U"...", UR"zzz(...)zzz", L"...", or LR"(...)", respectively.
encoding-prefixopt " s-char-sequenceopt "
encoding-prefixopt R raw-string
example: "me"
, L"you"
boolean-literal
The Boolean literals are the keywords false
and true
. Such literals are prvalues
and have type bool
.
false
true
pointer-literal
The pointer literal is the keyword nullptr
. It is a prvalue of type std::nullptr_t
.
nullptr
user-defined-literal
Overload of operator""
that allows to interpret literal using user-defined logic.
example:
typedef double signalf;
constexpr signalf operator"" _percent( long double val) { return val / 100 ; }
std::cout << 12.34_percent; // prints 0.1234
There's no literal for int*
. The closest you get is nullptr
, but its type is nullptr_t
.
Most types do not have a literal. Classes do not have one. Pointers, references, r-value references have none, as well. void
does not have a literal.
You mentioned string
which also does not have a literal, it only has a constructor that takes a const char*
literal! C++ calls this constructor implicitly, when assigning a const char*
literal to a string.
Adding to the list, there's no literals for signed char
, unsigned char
, short
, unsigned short
.
You should probably think in terms of the limited list of types for which there are literals, rather than the types for which there are not. Literals are for:
- bool
- char, wchar_t, char16_t, char32_t
- "array of const" char, wchar_t, char16_t, char32_t (that is, string literals)
- int, unsigned int
- long, unsigned long
- long long, unsigned long long
- float, double, long double
- std::nullptr_t
plus user-defined literals.
First of all type std::string
has no its own literals.
Only arithmetic types have their own literals. The exception is string literals that has type of const char arrays
and not so fer introduced in the C++ 11 nullptr
of type std::nullptr_t
Take into account that bool
is an arithmetic type.
Bjarne Stroustrup's Programming Principles and Practice Using C++ (Sixth Printing, November 2012) refers to ISO/IEC 14882:2003 as the relevant C++ standard. In this can be found the pertinent section, §2.13, which reads as follows:
2.13 Literals
There are several kinds of literals.
literal:
integer-literal
character-literal
floating-literal
string-literal
boolean-literal
Apparently, this is an exhaustive list; and because there can be many more types than there are kinds of literal in that list, the answer is: no, not every type has a corresponding literal.
N.B. for the C++11 case, please see Lizusek's answer.