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?
There's no literal for
int*
. The closest you get isnullptr
, but its type isnullptr_t
.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:
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.
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
andtrue
. Such literals areprvalues
and have typebool
.pointer-literal
The pointer literal is the keyword
nullptr
. It is a prvalue of typestd::nullptr_t
.user-defined-literal
Overload of
operator""
that allows to interpret literal using user-defined logic.example:
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:
plus user-defined literals.
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 aconst char*
literal! C++ calls this constructor implicitly, when assigning aconst char*
literal to a string.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 ofconst char arrays
and not so fer introduced in the C++ 11nullptr
of typestd::nullptr_t
Take into account that
bool
is an arithmetic type.