title: Get type from variant at compile time
body: <p>I need to do a typecheck on whether a variant type can hold a type at compile time.</p>
  
  <p>I am converting an enum and a string to a variant, but I want the library to be compatible with a user provided variant (for the types they support). So I have a template parameter <code>CustomVariant</code> to represent a variant over a subset of the supported types, <code>AlphaBeta</code>, <code>Gamma</code>, <code>Delta</code>, and <code>Epsilon</code>. I would like to return <code>std::nullopt</code> if I can't create a valid variant. </p>
  
  <pre><code>template &lt;typename CustomVariant&gt;
  std::optional&lt;CustomVariant&gt; AsCustomVariant(LargeEnum type, const std::string&amp; name) {
    case LargeEnum::ALPHA:
    case LargeEnum::BETA:
      return ConvertAlphaBeta(name);
  
    case LargeEnum::GAMMA:
      return ConvertGamma(name);
  
    case LargeEnum::DELTA:
      return ConvertDelta(name);
  
    case LargeEnum::EPSILON:
      return ConvertEpsilon(name);
  
    default:
      return std::nullopt;
  }
  </code></pre>
  
  <p>The idea is to use some sort of template magic that can do something like:</p>
  
  <pre><code>if (std::type_can_convert&lt;CustomVariant, Gamma&gt;) {
    return ConvertGamma(name);
  } else {
    return std::nullopt;
  }
  </code></pre>