Using an interactive shell (REPL)

Using an interactive shell (REPL)

How to:

C++ doesn’t come with a built-in REPL, but tools like Cling offer that capability. Here’s how to use Cling to calculate the sum of two numbers:

#include <iostream>

int main() {
    int a = 5;
    int b = 7;
    std::cout << "The sum is: " << a + b << std::endl;
    return 0;

// Output:
// The sum is: 12

Start Cling and enter the code line by line, observing the output after each command. It’s immediate feedback, without compiling.

Deep Dive

REPLs are common for languages like Python or Lisp, and they’ve been around since the 1960s. For C++, a compiled language, the concept doesn’t fit as naturally, which is why tools like Cling exist—they interpret C++ on the fly. Alternatives include online compilers or small-scale test programs compiled traditionally. Cling is built on top of LLVM and Clang, providing a bridge for C++ to be used in an interpreted fashion.

See Also

  • Cling: An interactive C++ interpreter, built on the top of LLVM and Clang libraries.
  • Jupyter Notebooks: Offers an interactive shell within a notebook environment, supports C++ through the xeus-cling kernel.
  • LLVM: A collection of modular and reusable compiler and toolchain technologies, which Cling builds upon.