title: How do I resolve this <unresolved overloaded function type> error when using std::function?
body: <p>In the following (working) code example, the templated register_enum() function is used to iterate over an enumeration and call a user provided callback to convert an enum value to a c-string. All  enumerations are defined within a class, and enum to string conversion is done with a static to_cstring( enum ) function. When a class (like the shader class below) has more than one enumeration and corresponding overloaded to_cstring( enum ) function, the compiler can't decide which is the correct to_cstring() function to pass to register_enum(). I think the code explains better than I can...</p>
  
  <pre><code>#include &lt;functional&gt;
  #include &lt;iostream&gt;
  
  // Actual code uses Lua, but for simplification
  // I'll hide it in this example.
  typedef void lua_State;
  
  class widget
  {
      public:
          enum class TYPE
          {
              BEGIN = 0,
              WINDOW = BEGIN,
              BUTTON,
              SCROLL,
              PROGRESS,
              END
          };
  
          static const char *to_cstring( const TYPE value )
          {
              switch ( value )
              {
                  case TYPE::WINDOW: return "window";
                  case TYPE::BUTTON: return "button";
                  case TYPE::SCROLL: return "scroll";
                  case TYPE::PROGRESS: return "progress";
                  default: break;
              }
              return nullptr;
          }
  };
  
  class shader
  {
      public:
          enum class FUNC
          {
              BEGIN = 0,
              TRANSLATE = BEGIN,
              ROTATE,
              SCALE,
              COLOR,
              COORD,
              END
          };
  
          enum class WAVEFORM
          {
              BEGIN = 0,
              SINE = BEGIN,
              SQUARE,
              TRIANGLE,
              LINEAR,
              NOISE,
              END
          };
  
          static const char *to_cstring( const FUNC value )
          {
              switch ( value )
              {
                  case FUNC::TRANSLATE: return "translate";
                  case FUNC::ROTATE: return "rotate";
                  case FUNC::SCALE: return "scale";
                  case FUNC::COLOR: return "color";
                  case FUNC::COORD: return "coord";
                  default: break;
              }
              return nullptr;
          }
  
          static const char *to_cstring( const WAVEFORM value )
          {
              switch ( value )
              {
                  case WAVEFORM::SINE: return "sine";
                  case WAVEFORM::SQUARE: return "square";
                  case WAVEFORM::TRIANGLE: return "triangle";
                  case WAVEFORM::LINEAR: return "linear";
                  case WAVEFORM::NOISE: return "noise";
                  default: break;
              }
              return nullptr;
          }
  };
  
  // Increment an enum value.
  // My compiler (g++ 4.6) doesn't support type_traits for enumerations, so
  // here I just static_cast the enum value to int... Something to be fixed
  // later...
  template &lt; class E &gt;
  E &amp;enum_increment( E &amp;value )
  {
      return value = ( value == E::END ) ? E::BEGIN : E( static_cast&lt;int&gt;( value ) + 1 );
  }
  
  widget::TYPE &amp;operator++( widget::TYPE &amp;e )
  {
      return enum_increment&lt; widget::TYPE &gt;( e );
  }
  
  shader::FUNC &amp;operator++( shader::FUNC &amp;e )
  {
      return enum_increment&lt; shader::FUNC &gt;( e );
  }
  
  shader::WAVEFORM &amp;operator++( shader::WAVEFORM &amp;e )
  {
      return enum_increment&lt; shader::WAVEFORM &gt;( e );
  }
  
  
  // Register the enumeration with Lua
  template&lt; class E &gt;
  void register_enum( lua_State *L, const char *table_name, std::function&lt; const char*( E ) &gt; to_cstring )
  {
      (void)L; // Not used in this example.
      // Actual code creates a table in Lua and sets table[ to_cstring( i ) ] = i
      for ( auto i = E::BEGIN; i &lt; E::END; ++i )
      {
          // For now, assume to_cstring can't return nullptr...
          const char *key = to_cstring( i );
          const int value = static_cast&lt;int&gt;(i);
          std::cout &lt;&lt; table_name &lt;&lt; "." &lt;&lt; key &lt;&lt; " = " &lt;&lt; value &lt;&lt; std::endl;
      }
  }
  
  int main( int argc, char **argv )
  {
      (void)argc; (void)argv;
  
      lua_State *L = nullptr;
  
      // Only one to_cstring function in widget class so this works...
      register_enum&lt; widget::TYPE &gt;( L, "widgets", widget::to_cstring );
  
      // ... but these don't know which to_cstring to use.
      register_enum&lt; shader::FUNC &gt;( L, "functions", shader::to_cstring );
      //register_enum&lt; shader::WAVEFORM &gt;( L, "waveforms", shader::to_cstring );
  
      return 0;
  }
  </code></pre>
  
  <p>Compiler output:</p>
  
  <pre><code>$ g++ -std=c++0x -Wall -Wextra -pedantic test.cpp -o test &amp;&amp; ./test
  test.cpp: In function ‘int main(int, char**)’:
  test.cpp:140:69: error: no matching function for call to ‘register_enum(lua_State*&amp;, const char [10], &lt;unresolved overloaded function type&gt;)’
  test.cpp:140:69: note: candidate is:
  test.cpp:117:7: note: template&lt;class E&gt; void register_enum(lua_State*, const char*, std::function&lt;const char*(E)&gt;)
  </code></pre>
  
  <p>How do I pass the correct to_cstring function to register_enum()? I realise I could rename the individual to_cstring() functions but I'd like to avoid this if possible. Perhaps my design is smelly and you can recommend a better approach.</p>
  
  <p>My question appears similar to <a href="https://stackoverflow.com/q/8970152/331024">Calling overloaded function using templates (unresolved overloaded function type compiler error)</a> and <a href="https://stackoverflow.com/q/705854/331024">How to get the address of an overloaded member function?</a> but so far I am unable to apply that information to my specific issue.</p>