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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:46:50+00:00 2026-05-16T00:46:50+00:00

If I have an enum like this: enum Errors { ErrorA = 0, ErrorB,

  • 0

If I have an enum like this:

enum Errors {
    ErrorA = 0,
    ErrorB,
    ErrorC,
};

Then I want to print it out to console:

Errors anError = ErrorA;
std::cout << anError; // 0 will be printed

But what I want is the text "ErrorA". Can I do it without using if/switch? And what is your solution for this?

  • 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-05-16T00:46:51+00:00Added an answer on May 16, 2026 at 12:46 am

    Using map:

    #include <map>
    #include <string_view>
    
    enum Errors {ErrorA=0, ErrorB, ErrorC};
    
    std::ostream& operator<<(std::ostream& out, const Errors value){
        // prior to C++11, create a function outside instead of using a lambda
        static const auto strings = []{
            // prior to C++17, replace std::string_view with
            // std::string or const char*
            std::map<Errors, std::string_view> result;
        #define INSERT_ELEMENT(p) result.emplace(p, #p);
            INSERT_ELEMENT(ErrorA);     
            INSERT_ELEMENT(ErrorB);     
            INSERT_ELEMENT(ErrorC);             
        #undef INSERT_ELEMENT
            return result;
        };
        
        return out << strings[value];
    }
    

    Using array of structures with linear search:

    #include <string_view>
    
    enum Errors {ErrorA=0, ErrorB, ErrorC};
    
    std::ostream& operator<<(std::ostream& out, const Errors value){
    #define MAPENTRY(p) {p, #p}
        const struct MapEntry{
            Errors value;
            std::string_view str;
        } entries[] = {
            MAPENTRY(ErrorA),
            MAPENTRY(ErrorB),
            MAPENTRY(ErrorC),
            {ErrorA, 0}//doesn't matter what is used instead of ErrorA here...
        };
    #undef MAPENTRY
        const char* s = 0;
        for (const MapEntry* i = entries; i->str; i++){
            if (i->value == value){
                s = i->str;
                break;
            }
        }
    
        return out << s;
    }
    

    Using switch/case:

    #include <string>
    
    enum Errors {ErrorA=0, ErrorB, ErrorC};
    
    std::ostream& operator<<(std::ostream& out, const Errors value){
        return out << [value]{
        #define PROCESS_VAL(p) case(p): return #p;
            switch(value){
                PROCESS_VAL(ErrorA);     
                PROCESS_VAL(ErrorB);     
                PROCESS_VAL(ErrorC);
            }
        #undef PROCESS_VAL
        };
    }
    

    Test the solutions:

    #include <iostream>
    
    int main(int argc, char** argv){
        std::cout << ErrorA << std::endl << ErrorB << std::endl << ErrorC << std::endl;
        return 0;   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like what you have now is the listener… May 16, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer There isn't a real URL attribute in the HTTPService --… May 16, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer You can use an assembly binding redirection specified in web.config.… May 16, 2026 at 12:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

If I have a simple class like this one for a card: class Card
I have my enum, which I don't want to share between layers. So I
I have an enum declaration using bit flags and I cant exactly figure out
Hi currently have the problem, that I want to insert values in a postgres
This might seem like a trivial question, but I'm a bit muddled in my
Say I have a method declaration like: private void someMethod(final String someKey, final Object
I have a problem with overriding the equals method in an Enum to make
I'm relatively confused about this... I've got a table like: +----------------+--------------------------------------------------+------+-----+-------------------+-----------------------------+ | Field |
I don’t know if it’s possible, but I want to be able to refer
Basically, I have a Linked List which implements a display() function that simply loops

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.