title: How to make a safer C++ variant visitor, similar to switch statements?
body: <p>The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: (<a href="http://en.cppreference.com/w/cpp/utility/variant/visit" rel="noreferrer">snippet from cppreference.com</a>)</p>
  
  <pre><code>std::variant&lt;int, long, double, std::string&gt; v = ...;
  
  std::visit(overloaded {
      [](auto arg) { std::cout &lt;&lt; arg &lt;&lt; ' '; },
      [](double arg) { std::cout &lt;&lt; std::fixed &lt;&lt; arg &lt;&lt; ' '; },
      [](const std::string&amp; arg) { std::cout &lt;&lt; std::quoted(arg) &lt;&lt; ' '; },
  }, v);
  </code></pre>
  
  <p>The problem is when you put the wrong type in the visitor or change the variant signature, but forget to change the visitor. Instead of getting a compile error, you will have the wrong lambda called, usually the default  one, or you might get an implicit conversion that you didn't plan. For example:</p>
  
  <pre><code>v = 2.2;
  std::visit(overloaded {
      [](auto arg) { std::cout &lt;&lt; arg &lt;&lt; ' '; },
      [](float arg) { std::cout &lt;&lt; std::fixed &lt;&lt; arg &lt;&lt; ' '; } // oops, this won't be called
  }, v);
  </code></pre>
  
  <p>Switch statements on enum classes are way more secure, because you can't write a case statement using a value that isn't part of the enum. Similarly, I think it would be very useful if a variant visitor was limited to a subset of the types held in the variant, plus a default handler. Is it possible to implement something like that?</p>
  
  <p>EDIT: s/implicit cast/implicit conversion/</p>
  
  <p>EDIT2: I would like to have a meaningful catch-all <code>[](auto)</code> handler. I know that removing it will cause compile errors if you don't handle every type in the variant, but that also removes functionality from the visitor pattern.</p>