Advertisement

Casting & Conversions interview questions and answers

12 Casting & Conversions interview questions for C++ roles, each with a model answer, examples and likely follow-ups.

12 questions
Advertisement

12 questions

Q11 Beginner Save

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

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.
Likely follow-ups
  • Does std::move perform the move? | What does it cast to?
Move Semantics Casting & Conversions
Q12 Advanced Save

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

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.
Likely follow-ups
  • How is it safer than reinterpret_cast? | What are its requirements?
Undefined Behavior Modern C++ Casting & Conversions

Showing 11–12 of 12