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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:52:45+00:00 2026-05-11T00:52:45+00:00

In the external code that I am using there is enum: enum En {VALUE_A,

  • 0

In the external code that I am using there is enum:

enum En {VALUE_A, VALUE_B, VALUE_C}; 

In another external code that I am using there are 3 #define directives:

#define ValA 5 #define ValB 6 #define ValC 7 

Many times I have int X which is equal to ValA or ValB or ValC, and I have to cast it to the corresponding value of En (ValA to VALUE_A, ValB to VALUEB, etc) because some function signature has enum En. And many times I have to do the opposite operation, translate enum En to ValA or ValB or ValC. I cannot change the signatures of these functions, and there are many such functions.

The question is: How to do the translation? Should I create 2 cast operators, which will be used implicitly? Or should I just have 2 translation functions which will be used explicitly:

En ToEn(int) int FromEn(En) 

Or any other solution?

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

    Since you can’t just cast here, I would use a free function, and if there are likely to be other enums that also need converting, try to make it look a little bit like the builtin casts:

    template<typename T> T my_enum_convert(int);  template<> En my_enum_convert<En>(int in) {     switch(in) {         case ValA: return VALUE_A;         case ValB: return VALUE_B;         case ValC: return VALUE_C;         default: throw std::logic_error(__FILE__ ': enum En out of range');     } }  int my_enum_convert(En in) {     switch(in) {         case VALUE_A: return ValA;         case VALUE_B: return ValB;         case VALUE_C: return ValC;         // no default, so that GCC will warn us if we've forgotten a case     } }  En enumValue = my_enum_convert<En>(ValA); int hashDefineValue = my_enum_convert(VALUE_A); enumValue = my_enum_convert<En>(0); // throws exception 

    Or something like that – might adjust it if issues arise while using it.

    The reason I wouldn’t use implicit conversion is that there already is an implicit conversion from En to int, which gives the wrong answer. Even if you can reliably replace that with something which gives the right answer, the resulting code won’t look as though it’s doing any conversion. IMO this will hinder anyone who later looks at the code more than typing a call to a conversion routine will hinder you.

    If you want converting to int and converting from int to look very different, then you could give the template and the function above different names.

    Alternatively, if you want them to look the same (and more like a static_cast), you could do:

    template<typename T> T my_enum_convert(En in) {     switch(in) {         case VALUE_A: return ValA;         case VALUE_B: return ValB;         case VALUE_C: return ValC;     } }  int hashDefineValue = my_enum_convert<int>(VALUE_A); 

    As written, T must have an implicit conversion from int. If you want to support T that only has an explicit conversion, use ‘return T(ValA);’ instead (or ‘return static_cast<T>(ValA);’, if you consider that single-arg constructors are C-style casts and hence impermissible).

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

Sidebar

Ask A Question

Stats

  • Questions 219k
  • Answers 219k
  • 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 My preferred way of writing presentations is now using a… May 12, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer OK, a little history as I remember it and some… May 12, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer Well, the reason this glitch was made possible is because… May 12, 2026 at 11:46 pm

Related Questions

I am working on a FYI page that has large list of top-tiered links.
What's the best way to achieve something like code folding, or the type of
In the attached flex code, I am trying to call a javascript function in
I have a Silverlight 2 application that is consuming a WCF service. As such,

Trending Tags

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

Top Members

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.