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

The Archive Base Latest Questions

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

I need a variant type that holds instances of any user-defined class. So I

  • 0

I need a variant type that holds instances of any user-defined class. So I use void*:

typedef boost::variant<void*, int, float, std::string> Tvariant;

I’ve created a wrapper class with a map:

typedef std::map<std::string, Tvariant> Tvalues;

Example of usage:

int x = 123;
attributes.set("int_var", x);
x = attributes.get<int>("int_var");

MyClass* obj = new MyClass();
attributes.set("void*_var", obj);
obj = static_cast<MyClass*>( attributes.get<void*>("void*_var") );
obj = attributes.cast<MyClass*>("void*_var"); // the same

There are 2 issues with this void* in variant class:

  1. Copying attributes with pointers to dynamically allocated memory is dangerous and error-prone.
  2. User can static_cast void* to WrongClass*, not to MyClass*. It compiles, but result is unpredicted.

Possible solutions:

  1. Use boost::shared_ptr< void* >.
  2. Remember typeid for all void* values (when they are added) in the std::map<void*, typeid> typeid_map. When user requests for void* value, casting it to any TClass*, let’s evaluate an assertion: assert(typeid_from_typeid_map == typeid(TClass*)).

Questions:
1. Are there other solutions to hold values of any user-defined class?
2. May be you will recommend better solutions for mentioned issues and point some other issues?

  • 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-20T00:15:41+00:00Added an answer on May 20, 2026 at 12:15 am

    Have you look at Boost::Any? This is something that carries with it compile time knowledge of the casting of the type while hiding what type it really is via the Any.

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

Sidebar

Related Questions

I need to find a better solution to pass the data type into boost::variant
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I need to store a VARIANT of type bstr in a stl vector. I'm
I have a 2D array of type Variant . The size and values that
I need to cast an any variable to an original type. I need to
I want to construct boost::variant s containing default-constructed values, specified with a type index
I have written a variant class, which will be used as the main type
If, for example, you're going to write a variant type class, you will naturally
To solve some problem I need to compute a variant of the pascal's triangle
Need help with a query that I wrote: I have three tables Company id

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.