Skip to content
Learn Netverks

Lesson

Step 25/36 69% through track

move-semantics

Move semantics

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Move semantics: the syntax, patterns, and safety habits you need before advancing in C++.

Modern C++ prefers unique_ptr and move semantics over raw new/delete—teams expect this in new code.

You will apply Move semantics in contexts like: Modern services and engines migrating off raw pointers to safer ownership.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When pointers, structs, and basic control flow from intermediate lessons are familiar.

Move semantics (C++11) transfer resources from one object to another without deep copying—critical for std::vector, std::string, and custom RAII types.

std::move

std::vector<int> a = {1, 2, 3};
std::vector<int> b = std::move(a);
// a is valid but empty/unspecified

std::move casts to rvalue reference—it does not move by itself; move constructors/assignments perform the transfer.

Rule of five

If you manage resources manually, consider destructor, copy/move constructors, and copy/move assignment. With rule of zero, rely on RAII members.

Important interview questions and answers

  1. Q: lvalue vs rvalue?
    A: lvalues have identity and persist; rvalues are temporaries or moved-from objects eligible for move.
  2. Q: When is move automatic?
    A: Returning locals, inserting temporaries—compiler elision and move often apply without explicit std::move.

Self-check

  1. What does std::move actually do?
  2. Name the rule-of-five special members.

Tip: Do not std::move from an object you still need—moved-from state is valid but unspecified.

Interview prep

What does std::move do?

Casts to rvalue reference to enable move construction or assignment—it does not move by itself.

Rule of five?

Destructor, copy/move constructor, copy/move assignment when manually managing resources.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • std::move meaning?
  • Rvalue ref?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump