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 7787459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:40:41+00:00 2026-06-01T20:40:41+00:00

I am trying to build a function that converts an item from an enum

  • 0

I am trying to build a function that converts an item from an enum to its corresponding string. The enums I use are fairly long, so I didn’t want to use a switch-case. I found a method using boost::unordered_map very convenient, but I don’t know how to make a default return (when there is no item matching the enum).

const boost::unordered_map<enum_type, const std::string> enumToString = boost::assign::map_list_of
(data_1, "data_1")
(data_2, "data_2");

I tried to create an additional function:

std::string convert(enum_type entry)
{
    if (enumToString.find(entry))       // not sure what test to place here, 
        return enumToString.at(entry);  //because the find method returns an iter
    else
        return "invalid_value";
}

I even tried something exceedingly wrong:

std::string convert(enum_type entry)
{
    try{
        return enumToString.at(entry);
    }
    catch(...){
        return "invalid_value";
    }
}

Result: evil “Debug” runtime error.
Can somebody give me a suggestion on how to either
1) find an easier method to convert enum to a string with the same name as the enum item
2) find a way to use already built boost methods to get a default value from a hash map (best option)
3) find what to place in the test to use a function that returns either the pair of the key-value, or a different string if the key is not found in the map.

Thank you very much.

  • 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-01T20:40:45+00:00Added an answer on June 1, 2026 at 8:40 pm

    For the conversion routine:

    std::string
    convert( enum_type entry )
    {
        boost::unordered_map<enum_type, std::string>::const_iterator
                            retval = enumToString.find();
        return retval == enumToString.end()
            ? "invalid_value"
            : retval->second;
    }
    

    Note that if the enum contains no assigned values, a simple char const* [] will do the trick.

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

Sidebar

Related Questions

I am trying to build a function that extracts information from a database and
I am trying to build an application that converts my old custom Ethernet logs
I am trying to build a function that will properly quote/escape an attribute in
I am trying to build a php function that would return an array with
I'm trying to build a jQuery function that will allow me to produce a
I'm trying to build some JavaScript in MooTools that fetches table row markup from
I am trying to write a function that converts between Maps with integer keys
I'm trying to build a function that calculates hashed for a file. The function
I am trying to build a function in python that yields values of two
I'm trying to build a Render Callback function that will load a variety of

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.