Explain C++ code

Paste modern C++ and Vibe explains the templates, smart pointers and object lifetimes in it.

documents.cpp

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe25 lines selected

Press Explain it for the sample, or paste your own code first.

What this C++ sample shows

templates
largest is written once for any T that can be copied and compared with >. largest(std::vector<int>{3, 9, 4}) makes the compiler generate an int version, which returns 9.
RAII
Document prints closing in its destructor. The destructor runs on its own when the unique_ptr that owns the document is destroyed at the end of main, so cleanup can't be forgotten.
move semantics
std::move(open.front()) hands ownership to current. The pointer left in the vector is now null, so the last line prints true notes.txt.
pointers
std::unique_ptr is a pointer that owns its object: one owner at a time, deleted automatically, and it can be moved but not copied.

Good to know

  • items.at(0) throws std::out_of_range on an empty vector, where items[0] would be undefined behavior.
  • explicit stops a plain std::string from turning into a Document by accident, for example when passed to a function that takes a Document.
  • The vector still holds one element after the move. It is an empty unique_ptr, and dereferencing it is undefined behavior.

Watch out: closing notes.txt prints once, after everything else. The document is destroyed exactly once, by whichever unique_ptr owns it when main ends, here current.

Explain C++ code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).