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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:35:35+00:00 2026-05-21T22:35:35+00:00

I have a mixture of C++ classes, some of which store a state variable

  • 0

I have a mixture of C++ classes, some of which store a state variable 0..9 as integer, others use ‘0’ … ‘9’. Currently I do:

enum { kOne = '1' };
class StoresValueAsInt {
    static int value;  // contains 0 ... 9
};
class StoresValueAsChar {
    static char value; // contains '0' ... '9'
};
class StoresValueAsChar {
    static char value;
};

template <typename X>
isOne() { return X::value == kOne; }

This allows me to write the buggy code isOne<StoresValueAsInt::value>(). Instead, I would like to have the compiler complain about this incorrect usage. I tried the following:

enum ValueInt {
    kOne = 1
};
enum ValueChar {
    kOne = '1'
};

class StoresValueAsInt {
    static ValueInt value;
};
class StoresValueAsChar {
    static ValueChar value;
};
class StoresValueAsChar2 {
    static ValueChar value;
};

However, this does not work, because kOne is visible at the namespace level, and thus it has conflicting declarations of kOne.

Is there a way to not have the enums declared in the namespace? Or a better approach here?

updated: Added what I currently do; hoping to clarify my use case.

  • 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-21T22:35:36+00:00Added an answer on May 21, 2026 at 10:35 pm

    As far as best practice, I’m really not sure what you are trying to accomplish here so I can’t point you in the right direction.

    However, to answer your question directly: Placing the enums in seperate namespaces would solve your issue:

    namespace IntVals {
       enum ValueInt {
          kOne = 1
       };
    }
    namespace CharVals {
       enum ValueChar {
          kOne = '1'
       };
    }
    
    class StoresValueAsInt {
       static ValueInt value;
    };
    class StoresValueAsChar {
       static ValueChar value;
    };
    

    Then you should be able to use the following:

    StoresValueAsInt::value == CharVals::kOne
    

    Though this is ugly, and I wish I understood better what you were going for to offer a more elegant solution than this.

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

Sidebar

Related Questions

In almost all of my classes, I have a mixture of properties and internal
I have web site which serves web services (a mixture of .asmx and WCF)
We have a mixture of products, some with specialPrice and some with Catalog Rules
I have a large solution which contains a mixture of C# and VB.Net projects.
I am building a basic app interface which will have a mixture of text,
I have a string which contains words in a mixture of upper and lower
I have some really big application mixture of c# and j#. Sometimes when I
I have a mixture of small and large projects in SVN. Some of them
I have a project which uses C++/CLI to implement a GUI and some background
I have a custom overlay class ( ImageOverlay ) which inherits from google.maps.OverlayView .

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.