‹ BackHN Continuity

Thread

C++ Insights – See your source code with the eyes of a Compiler

172 points · 36 comments · rramadass

  1. StilesCrisis · · focus · HN ↗
    I wish the readme example showed a lambda capture. Now that's where compiler magic REALLY shows up.
    1. sltkr · · focus · HN ↗
      C++ lambdas are just structs that overload operator(); once you realize that they become a lot less mysterious. But it's a great example of how this tool is useful to learn about those things (of course you could also read the C++ standard but who has time for that?)

      Here&#x27;s an example: <a href="https:&#x2F;&#x2F;cppinsights.io&#x2F;s&#x2F;f04b6896" rel="nofollow">https:&#x2F;&#x2F;cppinsights.io&#x2F;s&#x2F;f04b6896

      (That also shows how C++&#x27;s range-based for-loops work, which is similarly some syntactic suger for a fairly mundane for-loop, but details are interesting: unlike a typical for-loop, the loop variable is declared inside the loop body.)

      1. StilesCrisis · · focus · HN ↗
        I said shows a lambda _capture_. This involves the compiler automatically determining which variables cross the scope boundary, and injecting code for each one.
        1. frabert · · focus · HN ↗
          I&#x27;m curious to understand which part you think is particularly interesting. The compiler clearly knows which variables are declared in what scope, so what&#x27;s mysterious about figuring out which values need to be captured?
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.