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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:18:47+00:00 2026-05-27T15:18:47+00:00

Using C++, I’m trying to create a generic container class to handle multiple data

  • 0

Using C++, I’m trying to create a generic container class to handle multiple data types. It’s a common problem with a variety of solutions, but I’ve found nothing as… intuitive as I’ve grown accustomed to in languages like Python or even VB/VBA…

So here’s my scenario:

I’ve built a DataContainer class based on boost::any which I use to store multiple data types of multiple elements. I use a map declared as:

std::map<std::string, DataContainer* (or DataContainerBase*)>

where DataContainer is a class that encapsulates an object of the type:

std::list<boost::any>

along with convenience functions for managing / accessing the list.

However, in the end, I’m still forced to do type conversions outside the data container.

For example, if I were to store a list of int values in the map, accessing them would require:

int value = boost::any_cast<int>(map["myValue"]->get());

I’d rather the boost code be contained entirely within the data container structure, so I would only need type:

int value = map["myValue"]->get();

or, worst-case:

int value = map["myValue"]->get<int>();

Of course, I could enumerate my data types and do something like:

int value = map["myValue"]->get( TYPE_INT );

or write type-specific get() functions:

getInt(), getString(), getBool() ... 

The problem with the last two options is that they are somewhat inflexible, requiring me to declare explicitly each type I wish to store in the container. The any_cast solution (which I have implemented and works) I suppose is fine, it’s just… inelegant? I dunno. It seems I shouldn’t need to employ the internal mechanics externally as well.

As I see it, passing the value without declaring the value type in the call to the DataContainer member function would require a void* solution (which is undesirable for obvious reasons), and using a “get()” call would require (so far as I can tell) a “virtual template” member function defined at the base class level, which, of course, isn’t allowed.

As it is, I have a workable solution, and really, my use in this case is limited enough in scope that most any solutions will work well. But I am wondering if perhaps there’s a more flexible way to manage a generic, multi-type data container than this.

  • 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-27T15:18:48+00:00Added an answer on May 27, 2026 at 3:18 pm

    If you want to introduce some sugar for this:

    int value = boost::any_cast<int>(map["myValue"]->get());
    

    then you might want to make the get() function to return a proxy object, defined +- like this:

    struct Proxy {
        boost::any& value;
        Proxy(boost::any& value) : value(value) {}
    
        template<typename T>
        operator T() {
            return boost::any_cast<T>(value);
        }
    };
    

    Then this syntax would work:

    int value = map["myValue"]->get();
    // returns a proxy which gets converted by any_cast<int>
    

    However I recommend to keep things explicit and just use that syntax:

    int value = map["myValue"]->get<int>();
    

    Here get doesn’t return a proxy object with a template method, but is a template method itself (but does the same as the template conversion operator shown above).

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

Sidebar

Related Questions

Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using C#, I need a class called User that has a username, password, active
using namespace boost; class A {}; class B : public A {}; class X
Using Entity Framework CodeFirst, how do I create a created datetime column that gets
Using jQuery UI Resizable I'm trying to prevent resizing based on various rules. The
using VB.Net2010 I need to call a C# DLL The problem I have is
I am using Paperclip to handle profile photo uploads in my app. They upload
Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly

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.