Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:04:28+00:00 2026-06-01T11:04:28+00:00

In the following (working) code example, the templated register_enum() function is used to iterate

  • 0

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…

#include <functional>
#include <iostream>

// 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 < class E >
E &enum_increment( E &value )
{
    return value = ( value == E::END ) ? E::BEGIN : E( static_cast<int>( value ) + 1 );
}

widget::TYPE &operator++( widget::TYPE &e )
{
    return enum_increment< widget::TYPE >( e );
}

shader::FUNC &operator++( shader::FUNC &e )
{
    return enum_increment< shader::FUNC >( e );
}

shader::WAVEFORM &operator++( shader::WAVEFORM &e )
{
    return enum_increment< shader::WAVEFORM >( e );
}


// Register the enumeration with Lua
template< class E >
void register_enum( lua_State *L, const char *table_name, std::function< const char*( E ) > 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 < E::END; ++i )
    {
        // For now, assume to_cstring can't return nullptr...
        const char *key = to_cstring( i );
        const int value = static_cast<int>(i);
        std::cout << table_name << "." << key << " = " << value << 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< widget::TYPE >( L, "widgets", widget::to_cstring );

    // ... but these don't know which to_cstring to use.
    register_enum< shader::FUNC >( L, "functions", shader::to_cstring );
    //register_enum< shader::WAVEFORM >( L, "waveforms", shader::to_cstring );

    return 0;
}

Compiler output:

$ g++ -std=c++0x -Wall -Wextra -pedantic test.cpp -o test && ./test
test.cpp: In function ‘int main(int, char**)’:
test.cpp:140:69: error: no matching function for call to ‘register_enum(lua_State*&, const char [10], <unresolved overloaded function type>)’
test.cpp:140:69: note: candidate is:
test.cpp:117:7: note: template<class E> void register_enum(lua_State*, const char*, std::function<const char*(E)>)

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.

My question appears similar to Calling overloaded function using templates (unresolved overloaded function type compiler error) and How to get the address of an overloaded member function? but so far I am unable to apply that information to my specific issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:04:30+00:00Added an answer on June 1, 2026 at 11:04 am

    The error tells you that there are two potential overloads that could be used, and the compiler cannot decide for you. On the other hand, you can determine which one to use by using a cast:

    typedef const char *(*func_ptr)( shader::FUNC );
    register_enum< shader::FUNC >( L, "functions", (func_ptr)shader::to_cstring );
    

    Or without the typedef (in a harder to read one-liner):

    register_enum< shader::FUNC >( L, "functions", 
                 (const char *(*)( shader::FUNC ))shader::to_cstring );
    

    *Note that in function signatures, the top-level const gets removed.

    The next question is why did the compiler not find the appropriate overload by itself? The problem there is that in the call to register_enum you pass the type of the enum, and that determines the type of the std::function to be std::function< const char* ( shader::FUNC ) >, but std::function has a templated constructor, and before trying to infer the type of the argument to the constructor, the compiler must know which overload you want to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the following code working for a complex rails form with checkboxes. I'm
Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);
On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;
The following code is working great in a normal console application: private void button1_Click(object
Why is the following code not working? if(EventLog.Exists(Foo)) { EventLog.Delete(Foo); } if(EventLog.Exists(Foo) == false)
Why isn't the following piece of code working in IE8? <select> <option onclick=javascript: alert('test');>5</option>
can anybody tell me why is the following code not working? <script type=text/javascript src=../../Scripts/jquery.js></script>
i am using python 2.5.2 . The following code not working. def findValue(self, text,
I have the following PHP code, but it's not working. I don't see any
Ok, firstly I have the following code working.. although my question is this; should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.