Hasty Briefsbeta

Bilingual

C++: A prvalue is not a temporary

6 months ago
  • #Value Categories
  • #Temporaries
  • #C++
  • A prvalue isn’t necessarily a temporary; it represents 'the idea of the object' and only materializes into a temporary when necessary.
  • Lvalues cannot be moved from, while rvalues can. std::move(v) converts an lvalue to an rvalue (xvalue).
  • Prvalues (e.g., std::vector{1,2,3}) initialize objects directly without creating temporaries unless required (e.g., when binding to a reference).
  • Temporary materialization occurs when a prvalue must bind to a reference, creating an actual temporary object.
  • Function calls returning by value are prvalues and initialize objects directly, avoiding unnecessary copies or moves.
  • Prvalues optimize object initialization by avoiding temporaries unless absolutely necessary, without relying on optimization techniques.