title: "type-switch" construct in C++11
body: <p>All the time, I find myself doing something like this:</p>
  
  <pre><code>Animal *animal = ...
  if (Cat *cat = dynamic_cast&lt;Cat *&gt;(animal)) {
      ...
  }
  else if (Dog *dog = dynamic_cast&lt;Dog *&gt;(animal)) {
      ...
  }
  else { assert(false); }
  </code></pre>
  
  <p>Once I see closures in C++11, I wonder, is something like this possible?</p>
  
  <pre><code>Animal *animal = ...
  typecase(animal,
      [](Cat *cat) {
          ...
      },
      [](Dog *dog) {
          ...
      });
  </code></pre>
  
  <p>Implementing typecase should have been easy, but I keep running into a problem where it can't figure out the function's argument, so it can't know what to try to dynamic_cast to, because it's hard to deduce lambdas' parameters. Took a few days of searching google and SO, but finally figured it out, so I'll share my answer below.</p>