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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:09:40+00:00 2026-05-18T00:09:40+00:00

If have a union that currently foresees some simple data types: union Value {

  • 0

If have a union that currently foresees some simple data types:

union Value
   {
   const char *str;
   double d;
   long l;
   short s;
   bool b;
   void *reference;
   };

The union also allows more complex data types to be stored (like enumerations, references to instances, …), but all data is stored in one of the members of the Value union.

Because the data types can be complex, the Type is curently stored in a separate structure, which looks like this:

struct Type
   {
   short BaseType;            // one of TYPE_STRING, TYPE_LONG, TYPE_DOUBLE, ...
   char *referenceName;       // only used if BaseType==TYPE_REFERENCE
   Enumeration *enumeration;  // only used if BaseType==TYPE_ENUMERATION
   };

While restructing the application, I was thinking about integrating both structs into one class. Value would then become a pure interface IValue, and there would be subclasses/implementations for each of the different types.

The disadvantage of integrating the Value and Type is that Value cannot be simply copied anymore, but it always needs to be cloned. The advantage is that you are guaranteed never to lose the type of a value anymore.

If I keep Value and Type as separate classes, I would still change Type from one structure to a hierarchy. IType would be the interface, and for every type there would be a subclass/implementation, e.g. StringType, DoubleType, LongType, … and eveny MyEnumType, MyOtherEnumType, … The implementations of IType can then decide which value to use in the Value union.

Although copying types still requires a clone, I can at least copy the values without a clone. And in most cases, the values are copied much more than the types.

When keeping Value and Type separate, the whole concept is that you can copy the Value around as many times as you want, but if you want to get the actual underlying value, you need the Type.

Although the application is pure C++ (no .Net), I am willing to use .Net as inspiration for my developments, and I notice that in .Net you also have values that can easily be created on the stack, passed by value to functions, … without the need for cloning.

Templates are not a solution in my application since this would make about 50% of my application templated.

Questions:

  • Anyone experience with handling a generic Value and Type system in C++?
  • Does .Net have a ‘generic’ value and/or type concept that can be used as inspiration?
  • Any more arguments for keeping Value and Type in separate classes?
  • Any more arguments for merging Value and Type into one class?
  • 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-18T00:09:40+00:00Added an answer on May 18, 2026 at 12:09 am

    You should use a boost::variant or boost::any. That’s what they’re for. Using them doesn’t mean that you have to template your entire solution. Moreover, if you don’t need dynamic, run-time variance between types, then that IS what templates are for. The right tool for the right job – and this is templates for static variance, boost::variant for dynamic variance. There’s neither need nor reason for you to roll your own dynamic typing system.

    .NET has the design failing which is “object”, but failing that, they could also use the same kind of technique used in boost::variant, as far as I know their generics are powerful enough to do that kind of thing.

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

Sidebar

Related Questions

No related questions found

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.