The book I'm reading frequently mentions that a type has to be literal type to use in certain situations, such as types that can use constexpr. But the only definition given was that literal types are arithmetic, reference, or pointer types. But when we define a constexpr type, it seems like it has to be initialized with a literal or a variable that was originally initialized with a literal.
I'm confused with what qualifies as a literal type and what doesn't.
Literal types let you indicate that an expression is equal to some specific primitive value. For example, if we annotate a variable with type Literal["foo"] , mypy will understand that variable is not only of type str , but is also equal to specifically the string "foo" .
There are three types of integer literals in C programming: decimal (base 10) octal (base 8) hexadecimal (base 16)
it is an aggregate type or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and. all of its non-static data members and base classes are of non-volatile literal types.
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.
But the only definition given was that literal types are arithmetic, reference, or pointer types. But when we define a constexpr type, it seems like it has to be initialized with a literal or a variable that was originally initialized with a literal. I'm confused with what qualifies as a literal type and what doesn't.
A literal type is a set of just one value, it represents only one literal value: Using literal types with union types (used to compose types) you could define a type that represents an enumeration of literal values. For example, we can define the type Weather. The following literal types are included to the DataWeave type system:
But the only definition given was that literal types are arithmetic, reference, or pointer types. But when we define a constexpr type, it seems like it has to be initialized with a literal or a variable that was originally initialized with a literal.
You can use literal types inside Composite types (Array, Object, Function) to define a new type. For example, it can be used inside an Object type. This example defines a type detailedWeather as an Object type with a set of constraints for their key-value pairs.
From C++11, 3.9/10:
A type is a literal type if it is:
- a scalar type; or
- a reference type; or
- an array of literal type; or
- a class type (Clause 9) that has all of the following properties:
- it has a trivial destructor,
- every constructor call and full-expression in the brace-or-equal-initializers for non-static data members (if any) is a constant expression (5.19),
- it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
- all of its non-static data members and base classes are of literal types.
So basically it's either a reference, or a primitive object type, or something that can be constructed from a literal type in a constexpr-sort of way (arrays, aggregates, or classes with constexpr constructor).
C++11 (n3485), [basic.types]9-10
Arithmetic types, enumeration types, pointer types, pointer to member types,
std::nullptr_t
, and cv-qualified versions of these types are collectively called scalar types [...]A type is a literal type if it is:
- a scalar type; or
- a reference type; or
- an array of literal type; or
- a class type that has all of the following properties:
- it has a trivial destructor,
- every constructor call and full-expression in the brace-or-equal-initializers for non-static data members (if any) is a constant expression,
- it is an aggregate type or has at least one
constexpr
constructor or constructor template that is not a copy or move constructor, and- all of its non-static data members and base classes are of non-volatile literal types.
[dcl.init.aggr]/1
An aggregate is an array or a class with no user-provided constructors, no brace-or-equal-initializers for non-static data members, no private or protected non-static data members, no base classes, and no virtual functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With