Skip to content

Last updated:2026-10-12

C++ Insights Compile-Time Observation

Readers often ask me: what does a range-for actually compile into? Are lambda captures really just member variables? What if I cannot memorize the auto deduction rules? The answers to all of these questions are written in the compiler's eyes, but we normally cannot see them. Today I am introducing a tool that translates this "compiler-view" code straight out for you: C++ Insights.

1. What It Is

C++ Insights is an open-source tool by Andreas Fertig (GitHub); at heart it is a modified Clang: after semantic analysis of your C++ source, it "decompiles" it back into equivalent but fully explicit C++. It does not optimize and does not generate assembly — it exists solely to answer "what did the compiler write for me".

2. What You Can See

Take a few of the most-frequently-asked scenarios; I suggest opening the site and pasting the code below in to compare.

(1) the true face of range-for:

std::vector<int> v{1, 2, 3};
for (auto x : v) {
  std::cout << x;
}

Insights will tell you it actually is:

{
  auto&& __range = v;
  for (auto __begin = __range.begin(), __end = __range.end();
       __begin != __end; ++__begin) {
    auto x = *__begin;
    std::cout << x;
  }
}

This also explains why the loop has undefined behavior once __range dangles.

(2) a lambda is just an anonymous class's operator():

int base = 10;
auto add = [base](int x) { return base + x; };

After expansion you can see the compiler-generated closure type and the captured members; the difference between mutable, capture-by-reference, and capture-by-value is plain at a glance.

(3) auto/decltype, structured bindings, the real types after template instantiation — all made explicit.

3. How to Use

Online version: cppinsights.io, paste code on the left and get the result on the right, with a choice of C++ standard (11/14/17/20/23).

The command-line version can be installed locally:

git clone https://github.com/andreasfertig/cppinsights
cd cppinsights && mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j8

The compiled insights binary works directly on files: insights hello.cpp -- -std=c++20.

4. Summary

When writing articles, preparing talks, or getting ready for interviews, Insights is the fastest way to verify "what the compiler actually did" — far more reliable than memorizing rules. Questions are welcome in the comments~

Comments