title: How to sum over an array of variant using a visitor?
body: <p>I'm trying to find a way to sum over an <code>std::array</code> of <code>std::variant</code> using a visitor. I've gotten this far, but I can't for the life of me figure out how to deduce the type of the visitors without including a <code>void</code> entry at the head of my visitor lambda list.</p>
  
  <p>Does anyone know of a way I can deduce the return type of the lambdas in the visitor so that I don't have to rely on this?</p>
  
  <p>Here's what I've got right now:</p>
  
  <pre><code>#include &lt;array&gt;
  #include &lt;iostream&gt;
  #include &lt;string_view&gt;
  #include &lt;type_traits&gt;
  #include &lt;variant&gt;
  
  using namespace std::literals::string_view_literals;
  
  template&lt;typename... Base&gt;
  struct Visitor: Base ... {
      using Base::operator()...;
  };
  
  template&lt;typename... T&gt;
  Visitor(T...) -&gt; Visitor&lt;T...&gt;;
  
  // There has to be a better way to deduce Result than what I'm doing...
  template&lt;typename... T, typename S, typename... Ss, size_t N, typename Result = typename std::result_of_t&lt;S()&gt;&gt;
  constexpr std::enable_if_t&lt;std::is_arithmetic_v&lt;Result&gt;, Result&gt;
  summation(const Visitor&lt;S, Ss...&gt; &amp;visitor, const std::array&lt;std::variant&lt;T...&gt;, N&gt; &amp;array) {
      Result sum{};
      for (const auto &amp;a: array)
          sum += std::visit(visitor, a);
      return sum;
  }
  
  int main() {
      constexpr Visitor visitor {
              // This first entry should be unnecessary, I would think:
              []()                   -&gt; double { return 0; },
              [](double d)           -&gt; double { return d + 3.4; },
              [](int i)              -&gt; double { return i - 2; },
              [](std::string_view s) -&gt; double { return s.size(); }
      };
  
      constexpr std::array&lt;std::variant&lt;int, double, std::string_view&gt;, 5&gt; arr{9.0, 9, 3, 5.2, "hello world"sv};
      constexpr auto val = summation(visitor, arr);
      std::cout &lt;&lt; val &lt;&lt; '\n';
  }
  </code></pre>
  
  <p>Edit: I'd like the result to be <code>constexpr</code>.</p>
  
  <p>Thanks for any help.</p>