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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:58:22+00:00 2026-06-14T13:58:22+00:00

Note: I know similar questions to this have been asked on SO before, but

  • 0

Note: I know similar questions to this have been asked on SO before, but I did not find them helpful or very clear.

Second note: For the scope of this project/assignment, I’m trying to avoid third party libraries, such as Boost.

I am trying to see if there is a way I can have a single vector hold multiple types, in each of its indices. For example, say I have the following code sample:

vector<something magical to hold various types> vec;
int x = 3;
string hi = "Hello World";
MyStruct s = {3, "Hi", 4.01};

vec.push_back(x);
vec.push_back(hi);
vec.push_back(s);

I’ve heard vector<void*> could work, but then it gets tricky with memory allocation and then there is always the possibility that certain portions in nearby memory could be unintentionally overridden if a value inserted into a certain index is larger than expected.

In my actual application, I know what possible types may be inserted into a vector, but these types do not all derive from the same super class, and there is no guarantee that all of these types will be pushed onto the vector or in what order.

Is there a way that I can safely accomplish the objective I demonstrated in my code sample?

Thank you for your time.

  • 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-06-14T13:58:23+00:00Added an answer on June 14, 2026 at 1:58 pm

    In order to do that, you’ll definitely need a wrapper class to somehow conceal the type information of your objects from the vector.

    It’s probably also good to have this class throw an exception when you try to get Type-A back when you have previously stored a Type-B into it.

    Here is part of the Holder class from one of my projects. You can probably start from here.

    Note: due to the use of unrestricted unions, this only works in C++11. More information about this can be found here: What are Unrestricted Unions proposed in C++11?

    class Holder {
    public:
        enum Type {
            BOOL,
            INT,
            STRING,
            // Other types you want to store into vector.
        };
    
        template<typename T>
        Holder (Type type, T val);
    
        ~Holder () {
            // You want to properly destroy
            // union members below that have non-trivial constructors
        }
    
        operator bool () const {
            if (type_ != BOOL) {
               throw SomeException();
            }
            return impl_.bool_;
        }
        // Do the same for other operators
        // Or maybe use templates?
    
    private:
        union Impl {
            bool   bool_;
            int    int_;
            string string_;
    
            Impl() { new(&string_) string; }
        } impl_;
    
        Type type_;
    
        // Other stuff.
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know similar questions have been posted before, but I have specific requirements that
[NOTE: This questions is similar to but not the same as this one .]
Okay so I know I asked a similar question a while ago, but this
I know similar questions exists but they don't address this exact problem. I'm having
I know there are several questions similar, but I can't find one that answers
Note: I know there was disscussion about similar questions before, like in: link text
NOTE: I know there are many questions that talked about that but I'm still
Note: I know this is wrong , but this is a technical requirement by
Please note that I'm not asking how but why. And I don't know if
Note: this is NOT about concurrency. This is about the thread macro. I know

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.