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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:14:31+00:00 2026-05-26T17:14:31+00:00

In a C++ library whose internals I’m rewriting I have some unsigned integer variables

  • 0

In a C++ library whose internals I’m rewriting I have some unsigned integer variables that I want to convert to enums:

enum InitType {
    INIT,
    NON_INIT
};

and I have a variable of this type:

InitType InitVar;

The library is called from another section that of the code whose variables are plain integers:

uint32_t UnsignedIntVar;

I want to assign the unsigned version that was passed from the caller to the libraries internal enum:

InitVar = UnsignedIntVar;

But the compiler doesn’t like this:

error: invalid conversion from 'uint32_t' to 'InitType'

What is the cleanest way to perform this conversion?


Here are some ideas that I had:

If the enum has only two values, I can do it like this:

    InitVar = UnsignedIntVar ? Init : NonInit;

This is a lot of writing anytime I want to make such an assignment.

If it has more values, I can create a translation table:

InitType Uint2InitTypeConv = {INIT_0, INIT_1, INIT_2...};

Where INIT_x are just the names of the enums. Then I can translate using the table:

InitVar = Uint2InitTypeConv[UnsignedIntVar];

This seems pretty clean. However, I figure I should be able to overload operator= for this, but I can’t seem to be get that right. That would easily encapsulate any other ugliness I can come up with.

  • 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-26T17:14:32+00:00Added an answer on May 26, 2026 at 5:14 pm

    You can convert to enums explicitly:

    InitType i = InitType(UnsignedIntvar);
    

    If the integer doesn’t have a value that corresponds to a known enum value, that is your problem.

    A typical case where this is perfectly acceptable is a looping over enums:

    enum ESomething { Default = 0, Something, SomeOtherThing };
    for (int i = 0; i != 3; ++i)
    {
      ESomething e = ESomething(i);
      // do something with "e"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a c program that relies on a second library whose compilation i
I have a library-provided function that runs a callback after some processing. Inside that
I have an experimental library whose performance I'm trying to measure. To do this,
I have a problem with a Library whose assembly is strongly typed and for
I have a function from a library whose signature says:- public void setColumnNames(N... columnNames);
I have a double in Javascript whose value is, for example, 1.0883076389305e-311. I want
I have a custom helper whose class name, Image_Core conflicts with Kohana's Image Library
I have a library whose Junit 3 tests I'd like to reuse on Android.
We have written a large library functions whose prototypes mostly look like: void my_fun(
I would like to declare an external function from the kernel32.dll library whose name

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.