title: std::function as template parameter
body: <p>I currently have a <code>map&lt;int, std::wstring&gt;</code>, but for flexibility, I want to be able to assign a lambda expression, returning <code>std::wstring</code> as value in the map.</p>
  
  <p>So I created this template class:</p>
  
  <pre><code>template &lt;typename T&gt;
  class ValueOrFunction
  {
  private:
      std::function&lt;T()&gt; m_func;
  public:
      ValueOrFunction() : m_func(std::function&lt;T()&gt;()) {}
      ValueOrFunction(std::function&lt;T()&gt; func) : m_func(func) {}
      T operator()() { return m_func(); }
      ValueOrFunction&amp; operator= (const T&amp; other)
      {
          m_func = [other]() -&gt; T { return other; };
          return *this;
      }
  };
  </code></pre>
  
  <p>and use it like:</p>
  
  <pre><code>typedef ValueOrFunction&lt;std::wstring&gt; ConfigurationValue;
  std::map&lt;int, ConfigurationValue&gt; mymap;
  
  mymap[123] = ConfigurationValue([]() -&gt; std::wstring { return L"test"; });
  mymap[124] = L"blablabla";
  std::wcout &lt;&lt; mymap[123]().c_str() &lt;&lt; std::endl; // outputs "test"
  std::wcout &lt;&lt; mymap[124]().c_str() &lt;&lt; std::endl; // outputs "blablabla"
  </code></pre>
  
  <p>Now, I don't want to use the constructor for wrapping the lambda, so I decided to add a second assignment operator, this time for the <code>std::function</code>:</p>
  
  <pre><code>ValueOrFunction&amp; operator= (const std::function&lt;T()&gt;&amp; other)
  {
      m_func = other;
      return *this;
  }
  </code></pre>
  
  <p>This is the point where the compiler starts complaining. The line <code>mymap[124] = L"blablabla";</code> suddenly results in this error:</p>
  
  <blockquote>
    <p>error C2593: 'operator = is ambiguous'</p>
  </blockquote>
  
  <p>IntelliSense gives some more info:</p>
  
  <blockquote>
    <p>more than one operator "=" matches these operands: function
    "ValueOrFunction::operator=(const std::function &amp;other) [with
    T=std::wstring]" function "ValueOrFunction::operator=(const T
    &amp;other) [with T=std::wstring]" operand types are: ConfigurationValue =
    const wchar_t
    [10]  c:\projects\beta\CppTest\CppTest\CppTest.cpp    37  13  CppTest</p>
  </blockquote>
  
  <p>So, my question is, why isn't the compiler able to distinguish between <code>std::function&lt;T()&gt;</code> and <code>T</code>? And how can I fix this?</p>