Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c++ - Need clarification on definition of literal type

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...