title: Cannot use explicitly typed lambda
body: <p>I have this code:</p>
  
  <pre><code>std::function&lt;std::string&amp;(std::string&amp;)&gt; change_str = [](std::string&amp; str){
      return (str = "Hello world!");
  };
  
  std::string s;
  
  std::cout &lt;&lt; change_str(s) &lt;&lt; std::endl;
  </code></pre>
  
  <p>It does not compile, and say:</p>
  
  <pre><code>main.cpp:8:47: error: no viable conversion from '(lambda at main.cpp:8:60)' to 'std::function&lt;std::string &amp;(std::string &amp;)&gt;'
      std::function&lt;std::string&amp;(std::string&amp;)&gt; change_str = [](std::string&amp; str){
                                                ^            ~~~~~~~~~~~~~~~~~~~~~
  /usr/include/c++/v1/functional:1448:5: note: candidate constructor not viable: no known conversion from '(lambda at main.cpp:8:60)' to 'nullptr_t' for 1st argument
      function(nullptr_t) _NOEXCEPT : __f_(0) {}
      ^
  /usr/include/c++/v1/functional:1449:5: note: candidate constructor not viable: no known conversion from '(lambda at main.cpp:8:60)' to 'const std::__1::function&lt;std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; &amp;(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; &amp;)&gt; &amp;' for 1st argument
      function(const function&amp;);
      ^
  /usr/include/c++/v1/functional:1450:5: note: candidate constructor not viable: no known conversion from '(lambda at main.cpp:8:60)' to 'std::__1::function&lt;std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; &amp;(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; &amp;)&gt; &amp;&amp;' for 1st argument
      function(function&amp;&amp;) _NOEXCEPT;
      ^
  /usr/include/c++/v1/functional:1454:41: note: candidate template ignored: disabled by 'enable_if' [with _Fp = (lambda at main.cpp:8:60)]
                                          __callable&lt;_Fp&gt;::value &amp;&amp;
                                          ^
  main.cpp:8:60: note: candidate function
      std::function&lt;std::string&amp;(std::string&amp;)&gt; change_str = [](std::string&amp; str){
                                                             ^
  1 error generated.
  </code></pre>
  
  <p>However if I change the declaration of <code>std::function</code> to <code>auto</code>, then it works:</p>
  
  <pre><code>auto change_str = ...
  </code></pre>
  
  <p>Why is the explicit type not working for lambda?</p>