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

  • Home
  • SEARCH
  • 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 8356669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:15:22+00:00 2026-06-09T10:15:22+00:00

I need a container/wrapper C++ class which holds a single, arbitrary value. Once this

  • 0

I need a container/wrapper C++ class which holds a single, arbitrary value. Once this value is set only values of the same type should be accepted.

This is the code I’ve been experimenting with.

struct Genome {

struct FitnessConcept {};

template<typename T>
struct Fitness : public FitnessConcept{
    T value;        
    Fitness(T value) : value(value){}
};

std::shared_ptr<FitnessConcept> fitness;

template<typename T> 
void setFitness(T value) {
    fitness.reset(new Fitness<T>(value));               
}

template<typename T>
T getFitness() {
    return static_cast<Fitness<T>*>(fitness.get())->value;
}           
};

While Genome can hold arbitrary values, it does not restrict the type once the first is set, i.e. the following code is valid:

Genome g;
g.setFitness(0.2);
g.setFitness("foo"); //this should fail

UPDATE

Both compile and runtime failures are ok.

  • 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-09T10:15:23+00:00Added an answer on June 9, 2026 at 10:15 am

    Apart from using libraries like Boost.Any, a small modification of your source already works. Simply add a templated constructor to initialize the Genome, and have an equality test using the (implementation defined) typeid() operator between the new and old types.

    #include<stdexcept>
    #include<memory>
    #include<typeinfo>
    
    class Genome 
    {
    private:
            class FitnessConcept 
            {
            public:
                    virtual ~FitnessConcept() {}
            };
    
            template<typename T>
            class Fitness
            : 
                    public FitnessConcept
            {
            public:
                    explicit Fitness(T v)
                    : 
                            value_(v) 
                    {}      
    
                    T value() 
                    { 
                            return value_; 
                    }
    
            private:
                    T value_;                   
            };
    
    public:
            template<typename T>
            explicit Genome(T v)
            : 
                    fitness_(new Fitness<T>(v)) 
            {}
    
            template<typename T> 
            void setFitness(T v) 
            {
                    auto u = std::make_shared< Fitness<T> >(v);
                    if (typeid(fitness_).name() == typeid(u).name())
                            fitness_ = u;
                    else
                            throw std::invalid_argument("cannot change initialized genome type\n");
            }
    
            template<typename T>
            T getFitness() {
                    return static_cast<Fitness<T>*>(fitness_.get())->value();
            }
    
    private:
            std::shared_ptr<FitnessConcept> fitness_;           
    };
    
    int main()
    {
            Genome g(1.0);          // OK
            g.setFitness(2.0);      // OK
            g.setFitness("3.0");    // throws exception
    
            return 0;
    }
    

    Output on Ideone. There are several variations possible. E.g. if you do a dynamic_cast< u->get() >(fitness_->get()) then this will throw a std::bad_cast exception if the current underlying type of Genome is not convertible to the new type. This would allow you to change Genome to derived type but not to completely unrelated types.

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

Sidebar

Related Questions

for a small science project I set up a Simulation class which holds all
Ive got this problem: Line 20 (LOOT_FromContainer(container) I need that to use as Invoke
I'm working on an API wrapper class, which is the first I've made. For
After storing objects of different types in the same container using common parent class
I Created a wrapper for Ninject DI container which I intend to use in
I need to create a wrapper(container) with (from 2 to 4 div inside) like:
Following situation: I need a container for data with a rowindex. Currently we use
I am in need of a container that can handle duplicate keys like the
I need to append multiple nodes to a container. Rather than doing a slow
I need to create a live preview container (like stackoverflow and Reddit) for my

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.