C++

Casting & Conversions

12 question(s)

What is std::move and is it a cast?

Beginner
std::move doesn't move anything—it's a cast that unconditionally converts its argument to an rvalue reference (T&&), signaling that the object may be 'moved from'. This lets overload resolution pick move constructors/assignment. The actual moving is done by those move operations, not by std::move itself.
std::string a = "hi";
std::string b = std::move(a); // a is now moved-from
Real-world example std::move on a large temporary vector enables a cheap move instead of a deep copy into a container.

Common follow-ups: Does std::move perform the move? | What does it cast to?

Move Semantics Casting & Conversions Casting & Conversions

What is std::bit_cast (C++20)?

Advanced
std::bit_cast reinterprets the bit representation of one trivially-copyable type as another of the same size, safely and in a constexpr-friendly way—unlike reinterpret_cast or unions, it avoids undefined behavior and strict-aliasing issues. It's used for low-level conversions like reading a float's bits as an integer.
float f = 3.14f;
auto bits = std::bit_cast<std::uint32_t>(f); // safe bit reinterpretation
Real-world example std::bit_cast reads an IEEE float's raw bits for a hash without the UB of a reinterpret_cast.

Common follow-ups: How is it safer than reinterpret_cast? | What are its requirements?

Undefined Behavior Modern C++ Casting & Conversions