title: Compiler error with std::reference_wrapper and types in C++
body: <p>I've been diving into C++ lately, and I've been giving STL a go. I'm encountering a problem that I cannot seem to solve. I'm guessing it involves me not understanding how C++ deduces types, but I'm not sure. Here is the code that works</p>
  
  <pre><code>template &lt;typename T&gt;
  auto most_frequent_element(const std::vector&lt;std::reference_wrapper&lt;T&gt;&gt; &amp;vector) -&gt; boost::optional&lt;std::pair&lt;const std::reference_wrapper&lt;T&gt;, size_t&gt;&gt; {
      if (vector.empty()) {
          return{};
      }
      std::map&lt;std::reference_wrapper&lt;T&gt;, size_t&gt; individual_counts = {};
      std::for_each(vector.begin(), vector.end(), [&amp;individual_counts](auto &amp;element) {
          auto result = individual_counts.emplace(element, 0);
          (*result.first).second++;
       });
      return *std::max_element(individual_counts.begin(), individual_counts.end(), [](auto &amp;a, auto &amp;b) {
          return a.second &lt; b.second;
      });
  }
  </code></pre>
  
  <p>And this is how I might call it</p>
  
  <pre><code>auto main(int argc, char *argv[]) -&gt; int {
      std::vector&lt;char&gt; test = { 'a', 'a', 'b', 'b', 'c'};
      auto result = most_frequent_element(std::vector&lt;std::reference_wrapper&lt;char&gt;&gt;(test.begin(), test.end()));
      if (result) {
          std::cout &lt;&lt; (*result).first &lt;&lt; " " &lt;&lt; (*result).second &lt;&lt; std::endl;
      }
      else {
          std::cout &lt;&lt; "Empty input list." &lt;&lt; std::endl;
      }
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>So that works. But if I create a simple method to eliminate the need to copy the vector manualy, like so</p>
  
  <pre><code>template &lt;typename T&gt;
  auto most_frequent_element_2(const std::vector&lt;T&gt; &amp;vector) -&gt; boost::optional&lt;std::pair&lt;const std::reference_wrapper&lt;T&gt;, size_t&gt;&gt; {
      most_frequent_element(std::vector&lt;std::reference_wrapper&lt;T&gt;&gt;(vector.begin(), vector.end()));
  }
  </code></pre>
  
  <p>and call it like this</p>
  
  <pre><code>auto main(int argc, char *argv[]) -&gt; int {
      std::vector&lt;char&gt; test = { 'a', 'a', 'b', 'b', 'c'};
      auto result = most_frequent_element_2(test);
      if (result) {
          std::cout &lt;&lt; (*result).first &lt;&lt; " " &lt;&lt; (*result).second &lt;&lt; std::endl;
      }
      else {
          std::cout &lt;&lt; "Empty input list." &lt;&lt; std::endl;
      }
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I get the following error</p>
  
  <pre><code>Error C2664 'std::reference_wrapper&lt;char&gt;::reference_wrapper(std::reference_wrapper&lt;char&gt; &amp;&amp;)': cannot convert argument 1 from 'const char' to 'char &amp;'
  </code></pre>
  
  <p>I'm confused because how I see it they should do the same thing. If I am wrong can someone point me in the right direction.</p>
  
  <p>P.S. I'm working in Visual Studio 2015</p>
  
  <p><strong>Update:</strong> Added const constraint to return type of functions to correctly reflect the fact that the *max_element returns a const key. (suggested by @dyp)</p>