Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Enum class to string

I’m having a solution with switch cases but there are many cases so clang-tidy is giving warning for that function. My motive is to decrease the size of function. Is there any way that we can do to decrease size of function.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As enum class can be used as key for std::map, You can use the map to keep relation of enum <-> string, like this:

enum class test_enum { first, second, third };

const char* to_string(test_enum val) {
    static const std::map<test_enum,const char*> dict = {
        { test_enum::first, "first" },
        { test_enum::second, "second" },
        { test_enum::third, "third" }
    };
    
    auto tmp = dict.find(val);
    return (tmp != dict.end()) ? tmp->second : "<unknown>";
}

C++ has no reflection, so map cannot be filled automatically; however, using compiler-specific extensions (e.g. like __PRETTY_FUNCTION__ extension for GCC) it can be done, e.g. like in magic_enum library

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!