Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need clarification on definition of literal type

Tags:

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.

like image 489
Mars Avatar asked Sep 02 '13 22:09

Mars


People also ask

What does literal type mean?

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" .

What is the type of a literal C++?

There are three types of integer literals in C programming: decimal (base 10) octal (base 8) hexadecimal (base 16)

What is non literal type in C++?

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.

What type is a string literal?

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.

What qualifies as a literal type?

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.

What are literal types in dataweave?

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:

Are constexpr types initialized with a literal type?

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.

How do you use a literal type inside a composite type?

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.


2 Answers

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).

like image 144
Kerrek SB Avatar answered Oct 30 '22 12:10

Kerrek SB


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.

like image 36
dyp Avatar answered Oct 30 '22 11:10

dyp