C++ Move Semantics & Perfect Forwarding Quiz
40 in-depth questions covering C++ move semantics, rvalue references, move constructors, std::move, perfect forwarding with std::forward, and performance considerations — with 16 code examples to solidify understanding.
Question 1
What is the purpose of rvalue references in C++?
Question 2
What is the difference between lvalue and rvalue references?
int x = 42;
int& lref = x; // Lvalue reference
int&& rref = 42; // Rvalue reference to temporary
int&& rref2 = std::move(x); // Rvalue reference to moved lvalueQuestion 3
What is a move constructor?
class String {
char* data;
size_t size;
public:
String(String&& other) noexcept
: data(other.data), size(other.size) {
other.data = nullptr; // Steal resources
other.size = 0;
}
};Question 4
What is the purpose of std::move?
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = std::move(v1); // Move contents
// v1 is now emptyQuestion 5
What is perfect forwarding?
template<typename T>
void wrapper(T&& arg) {
func(std::forward<T>(arg)); // Preserve value category
}
wrapper(42); // Forwards as rvalue
int x = 42;
wrapper(x); // Forwards as lvalueQuestion 6
What is the difference between move assignment and copy assignment?
class String {
String& operator=(String&& other) noexcept {
if (this != &other) {
delete[] data; // Clean up current
data = other.data; // Steal resources
size = other.size;
other.data = nullptr; // Leave other empty
}
return *this;
}
};Question 7
What is a universal reference?
template<typename T>
void func(T&& arg) { // Universal reference
// arg can bind to both lvalues and rvalues
}
int x = 42;
func(x); // T=int&, arg is int& (lvalue)
func(42); // T=int, arg is int&& (rvalue)Question 8
What is reference collapsing?
Question 9
What is the difference between std::move and std::forward?
template<typename T>
void func(T&& arg) {
other_func(std::move(arg)); // Always rvalue
another_func(std::forward<T>(arg)); // Preserve category
}Question 10
What is move semantics performance benefit?
Question 11
What is the rule of five in C++11?
Question 12
What is the difference between rvalue and lvalue temporaries?
std::string create_string() {
return std::string("hello"); // Rvalue temporary
}
std::string s = create_string(); // Moves from temporary
std::string x = "world";
std::string y = x; // x is lvalue, copiesQuestion 13
What is the purpose of noexcept in move operations?
class String {
public:
String(String&& other) noexcept
: data_(other.data_), size_(other.size_) {
other.data_ = nullptr;
other.size_ = 0;
}
// noexcept enables move in containers
};Question 14
What is the difference between move and forward in template functions?
template<typename T>
std::unique_ptr<T> make_unique_move(T&& arg) {
return std::unique_ptr<T>(new T(std::move(arg))); // Always move
}
template<typename T>
void perfect_forward(T&& arg) {
func(std::forward<T>(arg)); // Preserve original category
}Question 15
What is copy elision and how does it relate to move semantics?
Question 16
What is the difference between rvalue reference and universal reference syntax?
void func(int&& arg) { } // Rvalue reference
template<typename T>
void func(T&& arg) { } // Universal reference
func(42); // Both bind to rvalue
int x = 42;
func(x); // Rvalue reference: error, universal: bindsQuestion 17
What is the difference between move constructor and move assignment operator?
Question 18
What is the difference between std::move and static_cast to rvalue reference?
std::vector<int> v;
std::vector<int> v2 = std::move(v); // Clear intent
std::vector<int> v3 = static_cast<std::vector<int>&&>(v); // Same effectQuestion 19
What is the difference between perfect forwarding and normal forwarding?
Question 20
What is the difference between move semantics and copy semantics?
Question 21
What is the difference between rvalue reference lifetime extension and move semantics?
const int& ref = 42; // Lifetime extended
int&& rref = 42; // Lifetime extended
std::string&& moved = std::move(some_string); // Move, no extensionQuestion 22
What is the difference between move-aware containers and regular containers?
Question 23
What is the difference between universal reference and rvalue reference overloading?
template<typename T>
void func(T&& arg); // Universal
void func(int& arg); // Lvalue
void func(int&& arg); // Rvalue
func(42); // Universal: T=int, perfect forwarding
// Overloads: calls int&& versionQuestion 24
What is the difference between move constructor and default constructor?
Question 25
What is the difference between std::forward and template argument deduction?
Question 26
What is the difference between move semantics and swap operations?
std::vector<int> v1 = {1,2,3};
std::vector<int> v2 = {4,5,6};
std::swap(v1, v2); // Swap contents
std::vector<int> v3 = std::move(v1); // Move contentsQuestion 27
What is the difference between rvalue reference and const lvalue reference binding?
void func(const int& x); // Binds to everything
void func(int&& x); // Only rvalues
func(42); // Both work, const& preferred
int y = 42;
func(y); // Only const& worksQuestion 28
What is the difference between move assignment and swap assignment?
String& operator=(String&& other) noexcept {
if (this != &other) {
delete[] data; // Clean current
data = other.data; // Take other's data
other.data = nullptr;
}
return *this;
}
String& operator=(String other) { // Copy and swap
swap(*this, other); // Swap with copy
return *this; // other cleans up
}Question 29
What is the difference between perfect forwarding and parameter passing?
Question 30
What is the difference between move semantics and RVO?
Question 31
What is the difference between universal reference and forwarding reference?
Question 32
What is the difference between move constructor and converting constructor?
Question 33
What is the difference between std::move and std::move_if_noexcept?
Question 34
What is the difference between rvalue reference and universal reference syntax?
using RRef = int&&; // Rvalue reference type
template<typename T>
using URef = T&&; // Universal reference type
RRef r = 42; // Only rvalues
URef<int&> ur = x; // Collapses to int&Question 35
What is the difference between move semantics and destructive operations?
Question 36
What is the difference between perfect forwarding and argument decay?
Question 37
What is the difference between move constructor and move assignment performance?
Question 38
What is the difference between std::forward and std::move in lambda captures?
auto lambda = [arg = std::move(obj)]() { }; // Move capture
auto forward_lambda = [](auto&&... args) {
return func(std::forward<decltype(args)>(args)...);
}; // ForwardQuestion 39
What is the difference between move semantics and placement new?
Question 40
What are the fundamental principles for effective move semantics in C++?
