title: using std::function in a std::map
body: <p>I have a class...</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;map&gt;
  #include &lt;boost/function.hpp&gt;
  
  enum class ECmd { one, two, three };
  
  class C
  {
  public:
  
      void Command(ECmd e)
      {
          auto pos = m_fnCmd.find(e);
  
          if (pos != m_fnCmd.end())
          {
              // call the function
              (pos-&gt;second)(this);
           }
          else
          {
              printf("no command!\n");
          }
      }
  
  protected:
      using fnCmd = boost::function&lt;void(C*)&gt;;
      using fnCmdMap = std::map&lt;ECmd, fnCmd&gt;;
  
      static const fnCmdMap m_fnCmd;
  
      // the command functions
      void One() { printf("one.\n"); }
      void Two() { printf("Two.\n"); }
      void Three() { printf("Three.\n"); }
  };
  
  const C::fnCmdMap C::m_fnCmd =
  {
      {std::make_pair(ECmd::one, &amp;C::One)},
      {std::make_pair(ECmd::two, &amp;C::Two)},
      {std::make_pair(ECmd::three, &amp;C::Three)},
  };
  </code></pre>
  
  <p>Which demonstrates a technique i use for processing id based commands. This code works just fine, however when i change the class to use std::function instead of boost::function it fails to compile - does not like initializing C::m_fnCmd.</p>
  
  <p>The error message is:</p>
  
  <pre><code>1&gt;c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(506): error C2664: 'void std::_Func_class&lt;_Ret,C *&gt;::_Set(std::_Func_base&lt;_Ret,C *&gt; *)' : cannot convert argument 1 from '_Myimpl *' to 'std::_Func_base&lt;_Ret,C *&gt; *'
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;          ]
  1&gt;          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class&lt;_Ret,C *&gt;::_Do_alloc&lt;_Myimpl,_Fret(__thiscall C::* const &amp;)(void),_Alloc&gt;(_Fty,_Alloc)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;  ,            _Fret=void
  1&gt;  ,            _Alloc=std::allocator&lt;std::_Func_class&lt;void,C *&gt;&gt;
  1&gt;  ,            _Fty=void (__thiscall C::* const &amp;)(void)
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class&lt;_Ret,C *&gt;::_Do_alloc&lt;_Myimpl,_Fret(__thiscall C::* const &amp;)(void),_Alloc&gt;(_Fty,_Alloc)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;  ,            _Fret=void
  1&gt;  ,            _Alloc=std::allocator&lt;std::_Func_class&lt;void,C *&gt;&gt;
  1&gt;  ,            _Fty=void (__thiscall C::* const &amp;)(void)
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class&lt;_Ret,C *&gt;::_Reset_alloc&lt;_Fret,C,,std::allocator&lt;std::_Func_class&lt;_Ret,C *&gt;&gt;&gt;(_Fret (__thiscall C::* const )(void),_Alloc)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;  ,            _Fret=void
  1&gt;  ,            _Alloc=std::allocator&lt;std::_Func_class&lt;void,C *&gt;&gt;
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class&lt;_Ret,C *&gt;::_Reset_alloc&lt;_Fret,C,,std::allocator&lt;std::_Func_class&lt;_Ret,C *&gt;&gt;&gt;(_Fret (__thiscall C::* const )(void),_Alloc)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;  ,            _Fret=void
  1&gt;  ,            _Alloc=std::allocator&lt;std::_Func_class&lt;void,C *&gt;&gt;
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class&lt;_Ret,C *&gt;::_Reset&lt;void,C,&gt;(_Fret (__thiscall C::* const )(void))' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;  ,            _Fret=void
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class&lt;_Ret,C *&gt;::_Reset&lt;void,C,&gt;(_Fret (__thiscall C::* const )(void))' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ret=void
  1&gt;  ,            _Fret=void
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\utility(157) : see reference to function template instantiation 'std::function&lt;void (C *)&gt;::function&lt;_From&gt;(_Fx &amp;&amp;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _From=void (__thiscall C::* )(void)
  1&gt;  ,            _Fx=void (__thiscall C::* )(void)
  1&gt;          ]
  1&gt;          c:\program files (x86)\microsoft visual studio 12.0\vc\include\utility(157) : see reference to function template instantiation 'std::function&lt;void (C *)&gt;::function&lt;_From&gt;(_Fx &amp;&amp;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _From=void (__thiscall C::* )(void)
  1&gt;  ,            _Fx=void (__thiscall C::* )(void)
  1&gt;          ]
  1&gt;          c:\projects\test\fntest\fntest.cpp(60) : see reference to function template instantiation 'std::pair&lt;const _Kty,_Ty&gt;::pair&lt;ECmd,void(__thiscall C::* )(void),void&gt;(std::pair&lt;ECmd,void (__thiscall C::* )(void)&gt; &amp;&amp;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Kty=ECmd
  1&gt;  ,            _Ty=C::fnCmd
  1&gt;          ]
  1&gt;          c:\projects\test\fntest\fntest.cpp(60) : see reference to function template instantiation 'std::pair&lt;const _Kty,_Ty&gt;::pair&lt;ECmd,void(__thiscall C::* )(void),void&gt;(std::pair&lt;ECmd,void (__thiscall C::* )(void)&gt; &amp;&amp;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Kty=ECmd
  1&gt;  ,            _Ty=C::fnCmd
  1&gt;          ]
  1&gt;
  </code></pre>